当前位置:网站首页>1704. judge whether the two halves of a string are similar

1704. judge whether the two halves of a string are similar

2022-06-24 08:52:00 Drag is me

leetcode Force button to brush questions and punch in

subject :1704. Determine whether the two halves of a string are similar
describe : Give you an even length string s . Split it into two halves with the same growth , The first half is a , The latter half is b .

Two strings be similar The premise is that they all contain the same number of vowels (‘a’,‘e’,‘i’,‘o’,‘u’,‘A’,‘E’,‘I’,‘O’,‘U’). Be careful ,s It may contain both upper and lower case letters .

If a and b be similar , return true ; otherwise , return false .

Their thinking

1、 The first a and b Take it out ;
2、 Calculation a and b Number of each vowel letter ;

Source code ##

class Solution {
    
public:
    bool isyy(char ch) {
    
        if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {
    
            return true;
        } else {
    
            return false;
        }
    }
    bool halvesAreAlike(string s) {
    
        int len = s.size();
        string a = s.substr(0, len / 2);
        string b = s.substr(len / 2);
        int cnta = 0, cntb = 0;
        for (int i = 0; i < len / 2; ++i) {
    
            if (isyy(a[i])) cnta++;
        }
        for (int i = 0; i < len / 2; ++i) {
    
            if (isyy(b[i])) cntb++;
        }
        return cnta == cntb;

    }
};
原网站

版权声明
本文为[Drag is me]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206240627022990.html