- Notifications
You must be signed in to change notification settings - Fork 135
/
Copy pathleetcode1023-camelcase-matching_word_frequency.cpp
85 lines (81 loc) · 2.63 KB
/
leetcode1023-camelcase-matching_word_frequency.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include<vector>
#include<algorithm>
#include<iostream>
#include<unordered_map>
usingnamespacestd;
classSolution {
public:
vector<bool> camelMatch(vector<string>& queries, string pattern) {
vector<bool> res;
vector<string> patArr = getCamelArr(pattern);
for (auto& q : queries)
{
auto qArr = getCamelArr(q);
bool check = true;
if (patArr.size() != qArr.size())
check = false;
else
{
for (int i = 0; i < patArr.size(); i++)
{
if (qArr[i][0] != patArr[i][0])
{
check = false;
break;
}
unordered_map<char, int> dict1; // 小写字母的词频
unordered_map<char, int> dict2;
for (auto c : qArr[i])
if (c >= 'a' && c <= 'z')
dict1[c]++;
for (auto c : patArr[i])
if (c >= 'a' && c <= 'z')
dict2[c]++;
for (char c = 'a'; c <= 'z'; c++)
{
if (dict2[c] > dict1[c])
{
check = false;
break;
}
}
}
}
res.push_back(check);
}
return res;
}
vector<string> getCamelArr(string s)
{
vector<string> wordList;
vector<int> upCharIndexes;
for (int i = 0; i < s.size(); i++)
{
// 找到大写字母的index
if (s[i] >= 'A' && s[i] <= 'Z')
upCharIndexes.push_back(i);
}
for (int i = 1; i < upCharIndexes.size(); i++)
{
string str = s.substr(upCharIndexes[i-1], upCharIndexes[i] - upCharIndexes[i-1]);
wordList.push_back(str);
}
wordList.push_back(s.substr(upCharIndexes.back(), s.size() - upCharIndexes.back()));
return wordList;
}
};
// Test
intmain()
{
Solution sol;
// vector<string> queries = {"uAxaqlzahfialcezsLfj", "cAqlzyahaslccezssLfj", "tAqlzahavslcezsLwzfj", "eAqlzbxahalcezelsLfj"};
// string pattern = "AqlzahalcezsLfj";
vector<string> queries = {"FooBar","FootBall"};
string pattern = "FoBa";
auto res = sol.camelMatch(queries, pattern);
for (auto match : res)
{
cout << (match ? "True" : "False") << endl;
}
return0;
}