Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. public class Solution {
  2. static bool[,] memo;
  3. public bool CheckValidString(string s) {
  4. memo = new bool[s.Length, s.Length];
  5. return Solve(s, 0, 0);
  6. }
  7.  
  8. public bool Solve(string s, int i, int balance){
  9. if(i == s.Length){
  10. return balance == 0;
  11. }
  12.  
  13. if(memo[i, balance]) return memo[i, balance];
  14.  
  15. if(s[i] == '(') memo[i, balance] = Solve(s, i + 1, balance + 1);
  16. else if(s[i] == ')') {
  17. if(balance <= 0) memo[i, balance] = false;
  18. else memo[i, balance] = Solve(s, i + 1, balance - 1);
  19. }
  20. else memo[i, balance] = Solve(s, i + 1, balance) || Solve(s, i + 1, balance + 1) || (balance != 0 &&Solve(s, i + 1, balance - 1));
  21.  
  22. return memo[i, balance];
  23. }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement