Guest User

Untitled

a guest
Oct 12th, 2022
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. class Solution {
  2. public:
  3.  
  4.  
  5.  
  6. void dfs(int x,int y,vector<int> &val )
  7. {
  8. // cout<<x<<" "<<y<<endl;
  9. if(val[x] or x==0)
  10. return;
  11. val[x]=y;
  12. for(int i=0;i<20;i++)
  13. if(x&(1<<i))
  14. dfs(x-(1ll<<i),y,val);
  15. }
  16. vector<int> solve(int N, vector<string> &S, int Q, vector<string> &Query) {
  17. // code here
  18.  
  19. vector<int> val(1500000);
  20. val[0]=1;
  21. for (int i = 0; i < N; i++)
  22. {
  23. int m = S[i].length(), ans = 0;
  24. vector<bool> cnt(20);
  25.  
  26. for (int j = 0; j < m; j++)
  27. {
  28. if (cnt[S[i][j] - 'a'] == 0)
  29. {
  30. cnt[S[i][j] - 'a'] = true;;
  31. }
  32. }
  33. for (int j = 0; j < 20; j++)
  34. {
  35. if (cnt[j])
  36. ans+=(1ll<<j);
  37.  
  38. }
  39. if(val[ans]==0)
  40. dfs(ans, i + 1,val);
  41. }
  42. vector<int> ans1;
  43. for(int i=0;i<Q;i++)
  44. {
  45. int ans=0;
  46. int m=Query[i].size();
  47. for(int j=0;j<m;j++)
  48. {
  49. ans+=(1<<(Query[i][j]-'a'));
  50. }
  51. ans1.push_back(val[ans]-1);
  52. }
  53. return ans1;
  54. }
  55. };
Advertisement
Add Comment
Please, Sign In to add comment