Skip to content

Commit

Permalink
Valid Parentheses
Browse files Browse the repository at this point in the history
  • Loading branch information
shamstabrez16 committed Oct 17, 2023
1 parent d1d55b5 commit 402034e
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 30 deletions.
40 changes: 40 additions & 0 deletions src/main/java/String/ValidParentheses.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package String;

import java.util.Stack;

public class ValidParentheses {
public boolean isValid(String s) {
Stack<Character> stack = new Stack<>();
int l = 0;
while (l<=s.length()-1){
Character input = s.charAt(l);
l++;
switch (input) {
case '{', '[', '(' -> stack.push(input);
case '}' -> {
if (stack.empty())
return false;
if (stack.pop() != '{') {
return false;
}
}
case ']' -> {
if (stack.empty())
return false;
if (stack.pop() != '[') {
return false;
}
}
case ')' -> {
if (stack.empty())
return false;
if (stack.pop() != '(') {
return false;
}
}
default -> throw new IllegalStateException("Unexpected value: " + input);
}
}
return stack.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import org.junit.Before;
import org.junit.Test;

public class TestBestTimeToBuyAndSellStock_121 {
public class TestBestTimeToBuyAndSellStock {

private BestTimeToBuyAndSellStock_121 bestTimeToBuyAndSellStock121;
@Before
Expand All @@ -12,13 +12,13 @@ public void setUp(){
}

@Test
public void TestCase1(){
public void testCase1(){
int[] prices ={7,1,5,3,6,4};
int expectedOutput = 5;
Assert.assertEquals(bestTimeToBuyAndSellStock121.maxProfit(prices),expectedOutput);
}
@Test
public void TestCase2(){
public void testCase2(){
int[] prices ={7,6,4,3,1};
int expectedOutput = 0;
Assert.assertEquals(bestTimeToBuyAndSellStock121.maxProfit(prices),expectedOutput);
Expand Down
7 changes: 3 additions & 4 deletions src/test/java/TestContainsDuplicate.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,17 @@ public void setup(){
containsDuplicate = new ContainsDuplicate();
}
@Test
public void TestCase1(){
public void testCase1(){
int[] nums={1,2,3,1};
boolean expected= true;
Assert.assertTrue(containsDuplicate.containsDuplicate(nums));
}
@Test
public void TestCase2(){
public void testCase2(){
int[] nums={1,2,3,4};
Assert.assertFalse(containsDuplicate.containsDuplicate(nums));
}
@Test
public void TestCase3(){
public void testCase3(){
int[] nums={1,1,1,3,3,4,3,2,4,2};
Assert.assertTrue(containsDuplicate.containsDuplicate(nums));
}
Expand Down
12 changes: 6 additions & 6 deletions src/test/java/TestFindMinimumInRotatedSortedArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,40 +12,40 @@ public void setUp() {
}

@Test
public void TestCase1() {
public void testCase1() {
int[] nums = {3, 4, 5, 1, 2};
int expectedOutput= 1;
Assert.assertEquals(expectedOutput, rotatedSortedArray.findMin(nums));
}
@Test
public void TestCase2() {
public void testCase2() {
int[] nums = {4, 5, 6, 7, 0, 1, 2};
int expectedOutput= 0;
Assert.assertEquals(expectedOutput, rotatedSortedArray.findMin(nums));
}

@Test
public void TestCase3() {
public void testCase3() {
int[] nums = {11, 13, 15, 17};
int expectedOutput= 11;
Assert.assertEquals(expectedOutput, rotatedSortedArray.findMin(nums));
}

@Test
public void TestCase4() {
public void testCase4() {
int[] nums = {3,1,2};
int expectedOutput= 1;
Assert.assertEquals(expectedOutput, rotatedSortedArray.findMin(nums));
}
@Test
public void TestCase5() {
public void testCase5() {
int[] nums = {2,1};
int expectedOutput= 1;
Assert.assertEquals(expectedOutput, rotatedSortedArray.findMin(nums));
}

@Test
public void TestCase6() {
public void testCase6() {
int[] nums = {5,1,2,3,4};
int expectedOutput= 1;
Assert.assertEquals(expectedOutput, rotatedSortedArray.findMin(nums));
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/TestMaximumProductSubarray.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@ public void SetUp() {
}

@Test
public void TestCase1() {
public void testCase1() {
int[] nums = {2, 3, -2, 4};
int expectedOutput = 6;
Assert.assertEquals(expectedOutput,maximumProductSubarray.maxProduct(nums));
}

@Test
public void TestCase2() {
public void testCase2() {
int[] nums = {-2,0,-1};
int expectedOutput = 0;
Assert.assertEquals(expectedOutput,maximumProductSubarray.maxProduct(nums));
}
@Test
public void TestCase3() {
public void testCase3() {
int[] nums = {2,-5,-2,-4,3};
int expectedOutput = 24;
Assert.assertEquals(expectedOutput,maximumProductSubarray.maxProduct(nums));
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/TestMaximumSubArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,26 @@ public void setUp() {
}

@Test
public void TestCase1(){
public void testCase1(){
int[] nums = {-2,1,-3,4,-1,2,1,-5,4};
int expected = 6;
Assert.assertEquals(maximumSubArray.maxSubArray(nums),expected);
}
@Test
public void TestCase2(){
public void testCase2(){
int[] nums = {1};
int expected = 1;
Assert.assertEquals(maximumSubArray.maxSubArray(nums),expected);
}
@Test
public void TestCase3(){
public void testCase3(){
int[] nums = {5,4,-1,7,8};
int expected = 23;
Assert.assertEquals(maximumSubArray.maxSubArray(nums),expected);
}

@Test
public void TestCase4(){
public void testCase4(){
int[] nums = {-1};
int expected = -1;
Assert.assertEquals(maximumSubArray.maxSubArray(nums),expected);
Expand Down
8 changes: 6 additions & 2 deletions src/test/java/TestSuite.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import String.ValidParentheses;
import String.ValidPalindrome;

@RunWith(Suite.class)
@Suite.SuiteClasses({
TestBestTimeToBuyAndSellStock_121.class,
TestBestTimeToBuyAndSellStock.class,
TestContainsDuplicate.class,
TestMaximumSubArray.class,
TestMaximumProductSubarray.class,
TestProductOfArrayExceptSelf.class,
TestTwoSum.class
TestTwoSum.class,
ValidParentheses.class,
ValidPalindrome.class
})
public class TestSuite {
}
7 changes: 3 additions & 4 deletions src/test/java/TestTwoSum.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import Array.MaximumProductSubarray;
import Array.TwoSum;
import org.junit.Assert;
import org.junit.Before;
Expand All @@ -13,21 +12,21 @@ public void SetUp() {
}

@Test
public void TestCase1() {
public void testCase1() {
int[] nums = {3,3};
int target = 6;
int[] expectedOutput ={0,1};
Assert.assertArrayEquals(expectedOutput,twoSum.twoSum(nums,target));
}
@Test
public void TestCase2() {
public void testCase2() {
int[] nums = {3,2,4};
int target = 6;
int[] expectedOutput ={1,2};
Assert.assertArrayEquals(expectedOutput,twoSum.twoSum(nums,target));
}
@Test
public void TestCase3() {
public void testCase3() {
int[] nums = {2,7,11,15};
int target = 9;
int[] expectedOutput ={0,1};
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/TestValidPalindrome.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,25 @@ public void setUp(){
}

@Test
public void TestCase1(){
public void testCase1(){
String s = "A man, a plan, a canal: Panama";
Assert.assertTrue(validPalindrome.isPalindrome(s));

}
@Test
public void TestCase2(){
public void testCase2(){
String s = " ";
Assert.assertTrue(validPalindrome.isPalindrome(s));
}
@Test
public void TestCase3(){
public void testCase3(){
String s = "race a car";
Assert.assertFalse(validPalindrome.isPalindrome(s));


}
@Test
public void TestCase4(){
public void testCase4(){
String s = "0P";
Assert.assertFalse(validPalindrome.isPalindrome(s));

Expand Down
38 changes: 38 additions & 0 deletions src/test/java/TestValidParentheses.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import String.ValidParentheses;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

public class TestValidParentheses {

private ValidParentheses validParentheses;

@Before
public void setUp(){
validParentheses = new ValidParentheses();
}


@Test
public void testCase1(){
String input = "()";
Assert.assertTrue(validParentheses.isValid(input));
}

@Test
public void testCase2(){
String input = "()[]{}";
Assert.assertTrue(validParentheses.isValid(input));
}

@Test
public void testCase3(){
String input = "(]";
Assert.assertFalse(validParentheses.isValid(input));
}
@Test
public void testCase4(){
String input = "]";
Assert.assertFalse(validParentheses.isValid(input));
}
}

0 comments on commit 402034e

Please sign in to comment.