[AlgoExpert] Words In Phone Number

Words In Phone Number

  • Time : O(w)
  • Space : O(|w|)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
unordered_map<char, char> mp{
{'a', '2'}, {'b', '2'}, {'c','2'},
{'d', '3'}, {'e', '3'}, {'f','3'},
{'g', '4'}, {'h', '4'}, {'i','4'},
{'j', '5'}, {'k', '5'}, {'l','5'},
{'m', '6'}, {'n', '6'}, {'o','6'},
{'p', '7'}, {'q', '7'}, {'r','7'}, {'s','7'},
{'t', '8'}, {'u', '8'}, {'v','8'},
{'w', '9'}, {'x', '9'}, {'y','9'}, {'z', '9'}
};
vector<string> wordsInPhoneNumber(string phoneNumber, vector<string> words) {
vector<string> res;
for(auto& w: words) {
string cvt = "";
for(auto& ch : w) {
if(isdigit(ch)) cvt.push_back(ch);
else cvt.push_back(mp[ch]);
}
auto pos = phoneNumber.find(cvt);
if(pos != string::npos) res.push_back(w);
}
return res;
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/05/29/PS/AlgoExpert/words-in-phone-number/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.