Skip to content

Commit

Permalink
Valid Parentheses.
Browse files Browse the repository at this point in the history
  • Loading branch information
PRATHAP KUDUPU authored and PRATHAP KUDUPU committed May 16, 2017
1 parent 36ed97c commit 41ea75b
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
Binary file added bin/Algorithms/Strings/ValidParentheses.class
Binary file not shown.
Binary file modified bin/HomeString.class
Binary file not shown.
41 changes: 41 additions & 0 deletions src/Algorithms/Strings/ValidParentheses.java
@@ -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();


}

}
11 changes: 11 additions & 0 deletions src/HomeString.java
Expand Up @@ -85,10 +85,21 @@ M is a thousand, CM is nine hundred (100-1000)
* Input: "do you like my blog"
Output: "golb ym ekil uoy od"
*/
/*
String str="do you like my blog";
String revStr=Algorithms.Strings.ReverseString3.cleanSolution(str);
System.out.println("Reverse a string : "+ revStr);
*/
/**
* Valid Parentheses
close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not
*/
String str="()[";

boolean valid=Algorithms.Strings.ValidParentheses.get(str);
System.out.println("Reverse a string : "+ valid);


}
}

0 comments on commit 41ea75b

Please sign in to comment.