-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d1d55b5
commit 402034e
Showing
10 changed files
with
110 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |