# Code language: Python classSolution: defhalvesAreAlike(self, s: str) -> bool: aeiou = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'] returnsum(c in aeiou for c in s[:len(s) // 2]) == sum(c in aeiou for c in s[len(s) // 2:])
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// Code language: Java classSolution { publicbooleanhalvesAreAlike(String s) { intcnt=0; s = s.toLowerCase(); for (inti=0, n = s.length(), sp = n / 2; i < n; ++i) { charc= s.charAt(i); if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') { if (i < sp) ++cnt; else --cnt; } } return cnt == 0; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// Code language: C++ classSolution { public: boolhalvesAreAlike(string s){ int cnt = 0; for (int i = 0, n = s.size(), sp = n / 2; i < n; ++i) { char c = s[i] | 32; if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') { if (i < sp) ++cnt; else --cnt; } } return cnt == 0; } };
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* Code language: JavaScript */ /** * @param {string} s * @return {boolean} */ var halvesAreAlike = function(s) { let cnt = 0; s = s.toLocaleLowerCase(); for (let i = 0, n = s.length, sp = n / 2; i < n; ++i) { let c = s[i]; if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') { if (i < sp) ++cnt; else --cnt; } } return cnt == 0; };
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* Code language: Go */ funchalvesAreAlike(s string)bool { cnt, n := 0, len(s) sp := n / 2 s = strings.ToLower(s) for i, c := range(s) { if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') { if (i < sp) { cnt++ } else { cnt-- } } } return cnt == 0 }