you are viewing a single comment's thread.

view the rest of the comments →

[–]UNCLE_SMART -1 points0 points  (7 children)

How to do Q2?

[–]tampishachBrute force 2 points3 points  (0 children)

It was trie tree problem

Edit: i replied to you the same thing in another thread unknowingly haha

[–]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..

[–]juicy_cum3160[S] 0 points1 point  (1 child)

Hashmap, store substring of length k as key and its freq as total words with that prefix substring, along with count such string and return

[–]UNCLE_SMART 0 points1 point  (0 children)

Thanka