Advertisement
SalmaYasser

Untitled

Jan 14th, 2020
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. class Solution {
  2. public:
  3. bool isSubsequence(string s, string t) {
  4.  
  5. unordered_map <char , vector <int>> char_idx;
  6.  
  7. for (int i = 0 ; i < t.size(); i++)
  8. {
  9. char c = t[i];
  10. char_idx[c].push_back(i);
  11. }
  12.  
  13. int last = -1;
  14.  
  15. for (int i = 0 ; i < s.size(); i++)
  16. {
  17. char c = s[i];
  18. vector <int> vec;
  19. if (char_idx.find(c) != char_idx.end()) vec = char_idx[c];
  20. else return false;
  21. int first_idx = upper_bound(vec.begin(), vec.end(), last) - vec.begin();
  22.  
  23. if (first_idx == vec.size())
  24. return false;
  25. last = vec[first_idx];
  26. }
  27.  
  28. return true;
  29.  
  30. }
  31. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement