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
PRATHAP KUDUPU
authored and
PRATHAP KUDUPU
committed
May 16, 2017
1 parent
36ed97c
commit 41ea75b
Showing
4 changed files
with
52 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
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,41 @@ | ||
package Algorithms.Strings; | ||
|
||
import java.util.Stack; | ||
|
||
/** | ||
* The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not. | ||
* @author Prathap Kudupu | ||
* | ||
*/ | ||
public class ValidParentheses { | ||
|
||
public static boolean get(String s) | ||
{ | ||
Stack<Character> stack= new Stack<Character>(); | ||
//Iterate through the string | ||
for(char c: s.toCharArray()) | ||
{ | ||
if(c =='(') | ||
{ | ||
stack.push(')'); | ||
} | ||
else if(c=='{') | ||
{ | ||
stack.push('}'); | ||
} | ||
else if(c=='[') | ||
{ | ||
stack.push(']'); | ||
} | ||
else if(stack.isEmpty() || stack.pop()!=c) | ||
{ | ||
return false; | ||
} | ||
} | ||
//It would return true if the stack is empty or it would return false | ||
return stack.isEmpty(); | ||
|
||
|
||
} | ||
|
||
} |
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