Guest User

Untitled

a guest
Nov 22nd, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.51 KB | None | 0 0
  1. class Solution {
  2. public:
  3. bool isValid(string s) {
  4. string left_parentheses(s.length()/2,'0');
  5. int i = 0, j = 0;
  6. while (s[i]) {
  7. if (s[i] == '(' || s[i] == '[' || s[i] == '{') {
  8. left_parentheses[j] = s[i];
  9. j++;
  10. }
  11. else if ((s[i] == ')' && left_parentheses[j - 1] == '(')
  12. || (s[i] == ']' && left_parentheses[j - 1] == '[')
  13. || (s[i] == '}' && left_parentheses[j - 1] == '{')) {
  14. j--;
  15. }
  16. else {
  17. return false;
  18. }
  19. i++;
  20. }
  21. if(j == 0) return true;
  22. return false;
  23. }
  24. };
Add Comment
Please, Sign In to add comment