Skip to content

Commit 27dd1e4

Browse files
committed
Add C++ solution for leetcode 1023.
1 parent d47c028 commit 27dd1e4

File tree

1 file changed

+85
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)