Advertisement
SalmaYasser

Untitled

Jan 3rd, 2020
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. class Solution {
  2. public:
  3.  
  4. pair <string , vector <int> > encode (string s)
  5. {
  6. string res;
  7. vector<int> cnts;
  8.  
  9. for(int i = 0; i < s.size(); i++){
  10. if(!i || s[i] != s[i-1]){
  11. res += s[i];
  12. cnts.push_back(1);
  13. } else if (s[i] == s[i-1]){
  14. cnts[cnts.size()-1]++;
  15. }
  16. }
  17. return {res, cnts};
  18. }
  19.  
  20. bool try_this (vector <int> d , vector <int> s)
  21. {
  22. for (int i = 0 ; i < s.size(); i++)
  23. {
  24. if (s[i] > d[i] || (s[i] < d[i] && d[i] < 3))
  25. return false;
  26. }
  27. return true;
  28. }
  29. int expressiveWords(string S, vector<string>& words) {
  30.  
  31. unordered_map < string , vector <vector <int> > > encodDict;
  32.  
  33. auto res_s = encode (S);
  34.  
  35. for (int i = 0 ; i < words.size(); i++)
  36. {
  37. auto s = encode(words[i]);
  38.  
  39. if (s.first == res_s.first)
  40. encodDict[res_s.first].push_back (s.second);
  41. }
  42.  
  43. int res = 0;
  44. for (vector <int> can : encodDict[res_s.first])
  45. {
  46. res += try_this (res_s.second, can);
  47. }
  48.  
  49. return res;
  50. }
  51. };
  52.  
  53. /*
  54.  
  55. W = hello
  56.  
  57. can = heeeellooo
  58.  
  59. dict [key] [vector <int>]
  60.  
  61. encode ("hello") [helo]["1,1,2,1"]
  62. encode ("heeeellooo")[helo] ["1,4,2,3"]
  63.  
  64. idx = 0 + 1 = 1
  65.  
  66. e [idx + 1, idx + (4 - 1)]
  67.  
  68. idx + 4 = 5
  69. idx + 5 = 7
  70. l [idx + 1 , idx + (3 - 1]
  71.  
  72.  
  73.  
  74.  
  75. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement