[InterviewBit] Anagrams

Anagrams

  • Time :
  • Space :
1
2
3
4
5
6
7
8
9
10
11
12
13
vector<vector<int>> Solution::anagrams(const vector<string> &A) {
unordered_map<string, vector<int>> mp;
for(int i = 0; i < A.size(); i++) {
string s = A[i];
sort(begin(s), end(s));
mp[s].push_back(i+1);
}
vector<vector<int>> res;
for(auto [_,v] : mp) res.push_back(v);
sort(begin(res), end(res));
return res;
}

Author: Song Hayoung
Link: https://songhayoung.github.io/2022/09/24/PS/interviewbit/anagrams/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.