fork(1) download
  1. class Solution {
  2. public:
  3.  
  4. void solve(string s,vector<string>&ans,unordered_map<string,bool>&mp,int removals)
  5. {
  6.  
  7. if(mp[s])
  8. return;
  9.  
  10. mp[s]=true;
  11.  
  12. if(removals==0)
  13. {
  14. //checking for valid answer
  15. int removals_needed=find_removals(s);
  16. if(removals_needed==0)
  17. ans.push_back(s);
  18.  
  19. return;
  20. }
  21.  
  22. for(int i=0;i<s.length();++i)
  23. {
  24. string leftpart=s.substr(0,i);
  25. string rightpart=s.substr(i+1);
  26. string join=leftpart+rightpart;
  27. solve(join,ans,mp,removals-1);
  28. }
  29.  
  30. return;
  31.  
  32. }
  33.  
  34. int find_removals(string s)
  35. {
  36. stack<char> st;
  37. for(int i=0;i<s.length();++i)
  38. {
  39. if(s[i]=='(')
  40. st.push(s[i]);
  41.  
  42. else if(s[i]==')')
  43. {
  44. if(st.size()!=0 && st.top()=='(')
  45. st.pop();
  46.  
  47. else
  48. st.push(')');
  49. }
  50. }
  51.  
  52. return st.size();
  53. }
  54.  
  55. vector<string> removeInvalidParentheses(string s) {
  56.  
  57. unordered_map<string,bool> mp;
  58. int min_removals=find_removals(s);
  59. vector<string> ans;
  60. solve(s,ans,mp,min_removals);
  61. return ans;
  62. }
  63. };
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:2: error: illegal start of type
public:
      ^
Main.java:4: error: <identifier> expected
    void solve(string s,vector<string>&ans,unordered_map<string,bool>&mp,int removals)
                                      ^
Main.java:4: error: illegal start of type
    void solve(string s,vector<string>&ans,unordered_map<string,bool>&mp,int removals)
                                                                     ^
Main.java:4: error: <identifier> expected
    void solve(string s,vector<string>&ans,unordered_map<string,bool>&mp,int removals)
                                                                        ^
Main.java:4: error: <identifier> expected
    void solve(string s,vector<string>&ans,unordered_map<string,bool>&mp,int removals)
                                                                         ^
Main.java:4: error: ';' expected
    void solve(string s,vector<string>&ans,unordered_map<string,bool>&mp,int removals)
                                                                                     ^
6 errors
stdout
Standard output is empty