-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1704.判断字符串的两半是否相似.cpp
80 lines (78 loc) · 2.06 KB
/
1704.判断字符串的两半是否相似.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
/*
* @lc app=leetcode.cn id=1704 lang=cpp
*
* [1704] 判断字符串的两半是否相似
*
* https://leetcode.cn/problems/determine-if-string-halves-are-alike/description/
*
* algorithms
* Easy (78.48%)
* Likes: 51
* Dislikes: 0
* Total Accepted: 36.2K
* Total Submissions: 46K
* Testcase Example: '"book"'
*
* 给你一个偶数长度的字符串 s 。将其拆分成长度相同的两半,前一半为 a ,后一半为 b 。
*
* 两个字符串 相似 的前提是它们都含有相同数目的元音('a','e','i','o','u','A','E','I','O','U')。注意,s
* 可能同时含有大写和小写字母。
*
* 如果 a 和 b 相似,返回 true ;否则,返回 false 。
*
*
*
* 示例 1:
*
*
* 输入:s = "book"
* 输出:true
* 解释:a = "bo" 且 b = "ok" 。a 中有 1 个元音,b 也有 1 个元音。所以,a 和 b 相似。
*
*
* 示例 2:
*
*
* 输入:s = "textbook"
* 输出:false
* 解释:a = "text" 且 b = "book" 。a 中有 1 个元音,b 中有 2 个元音。因此,a 和 b 不相似。
* 注意,元音 o 在 b 中出现两次,记为 2 个。
*
*
*
*
* 提示:
*
*
* 2 <= s.length <= 1000
* s.length 是偶数
* s 由 大写和小写 字母组成
*
*
*/
// @lc code=start
class Solution {
public:
bool halvesAreAlike(string s) {
unordered_map<string,string> hashtable{{"a","true"},{"e","true"},{"i","true"},{"o","true"},{"u","true"},{"A","true"},{"E","true"},{"I","true"},{"O","true"},{"U","true"}};
int length = s.size();
int a=0;
int b=0;
for(int i=0;i<length/2;++i){
const char item[] = { s[i], '\0' };
auto it=hashtable.find(item);
if (it!=hashtable.end()) {
++a;
}
}
for(int i=length/2;i<length;++i){
const char item[] = { s[i], '\0' };
auto it=hashtable.find(item);
if (it!=hashtable.end()) {
++b;
}
}
return a==b;
}
};
// @lc code=end