题目来源:
leetcode题目,网址:1704. 判断字符串的两半是否相似 - 力扣(LeetCode)
解题思路:
对前半段和后半段出现的元音字符分别计数,比较大小即可。
解题代码:
class Solution {
public boolean halvesAreAlike(String s) {
int count=0;
String vowels="aeiouAEIOU";
for(int i=0;i<s.length()/2;i++){
if(vowels.indexOf(s.charAt(i))>=0){
count++;
}
}
for(int i=s.length()/2;i<s.length();i++){
if(vowels.indexOf(s.charAt(i))>=0){
if(--count<0){
return false;
}
}
}
if(count==0)
return true;
return false;
}
}
总结:
官方题解也是一样的思路。
自减的优先级在 < 之上
vowels 元音