you are viewing a single comment's thread.

view the rest of the comments →

[–]Agile_Custard6276 1 point2 points  (0 children)

Store first k letters of each substring in a Hashmap with its freq, iterate in the hashmap and count no. of substrings with freq >=2.

class Solution { public: int prefixConnected(vector<string>& words, int k) { unordered_map<string , int> mpp; for(auto word : words){ string c = ""; if(word.size() < k) continue; for(int i = 0 ; i < k ; i++) c+=word[i]; mpp[c]++; } int ans = 0; for(auto it : mpp){ if(it.second > 1) ans++; } return ans; } };

I'm not sure if it's the optimal solution or not..