Advertisement
Guest User

Untitled

a guest
Oct 10th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. class Solution {
  2. public:
  3. bool isValid(string s) {
  4. string s1 = "";
  5.  
  6. for(int i=0;i<s.length();i++)
  7. {
  8. switch(s1.back())
  9. {
  10. case '(': if ( (s[i]=='}') || (s[i]==']') )
  11. return false;
  12. if ( s[i]==')' )
  13. {
  14. s1.pop_back();
  15. break;
  16. }
  17. s1.push_back(s[i]);
  18. break;
  19. case '{': if ( (s[i]==')') || (s[i]==']') )
  20. return false;
  21. if ( s[i]=='}' )
  22. {
  23. s1.pop_back();
  24. break;
  25. }
  26. s1.push_back(s[i]);
  27. break;
  28. case '[': if ( (s[i]==')') || (s[i]=='}') )
  29. return false;
  30. if ( s[i]==']' )
  31. {
  32. s1.pop_back();
  33. break;
  34. }
  35. s1.push_back(s[i]);
  36. break;
  37. default: s1.push_back(s[i]);
  38. }
  39.  
  40.  
  41. }
  42.  
  43. if (s1=="")
  44. return true;
  45. else return false;
  46.  
  47. }
  48. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement