Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- pair <string , vector <int> > encode (string s)
- {
- string res;
- vector<int> cnts;
- for(int i = 0; i < s.size(); i++){
- if(!i || s[i] != s[i-1]){
- res += s[i];
- cnts.push_back(1);
- } else if (s[i] == s[i-1]){
- cnts[cnts.size()-1]++;
- }
- }
- return {res, cnts};
- }
- bool try_this (vector <int> d , vector <int> s)
- {
- for (int i = 0 ; i < s.size(); i++)
- {
- if (s[i] > d[i] || (s[i] < d[i] && d[i] < 3))
- return false;
- }
- return true;
- }
- int expressiveWords(string S, vector<string>& words) {
- unordered_map < string , vector <vector <int> > > encodDict;
- auto res_s = encode (S);
- for (int i = 0 ; i < words.size(); i++)
- {
- auto s = encode(words[i]);
- if (s.first == res_s.first)
- encodDict[res_s.first].push_back (s.second);
- }
- int res = 0;
- for (vector <int> can : encodDict[res_s.first])
- {
- res += try_this (res_s.second, can);
- }
- return res;
- }
- };
- /*
- W = hello
- can = heeeellooo
- dict [key] [vector <int>]
- encode ("hello") [helo]["1,1,2,1"]
- encode ("heeeellooo")[helo] ["1,4,2,3"]
- idx = 0 + 1 = 1
- e [idx + 1, idx + (4 - 1)]
- idx + 4 = 5
- idx + 5 = 7
- l [idx + 1 , idx + (3 - 1]
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement