返回介绍

solution / 1700-1799 / 1704.Determine if String Halves Are Alike / README_EN

发布于 2024-06-17 01:03:15 字数 5976 浏览 0 评论 0 收藏 0

1704. Determine if String Halves Are Alike

中文文档

Description

You are given a string s of even length. Split this string into two halves of equal lengths, and let a be the first half and b be the second half.

Two strings are alike if they have the same number of vowels ('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'). Notice that s contains uppercase and lowercase letters.

Return true_ if _a_ and _b_ are alike_. Otherwise, return false.

 

Example 1:

Input: s = "book"
Output: true
Explanation: a = "bo" and b = "ok". a has 1 vowel and b has 1 vowel. Therefore, they are alike.

Example 2:

Input: s = "textbook"
Output: false
Explanation: a = "text" and b = "book". a has 1 vowel whereas b has 2. Therefore, they are not alike.
Notice that the vowel o is counted twice.

 

Constraints:

  • 2 <= s.length <= 1000
  • s.length is even.
  • s consists of uppercase and lowercase letters.

Solutions

Solution 1: Counting

Traverse the string. If the number of vowels in the first half of the string is equal to the number of vowels in the second half, return true. Otherwise, return false.

The time complexity is $O(n)$, where $n$ is the length of the string. The space complexity is $O(C)$, where $C$ is the number of vowel characters.

class Solution:
  def halvesAreAlike(self, s: str) -> bool:
    cnt, n = 0, len(s) >> 1
    vowels = set('aeiouAEIOU')
    for i in range(n):
      cnt += s[i] in vowels
      cnt -= s[i + n] in vowels
    return cnt == 0
class Solution {
  private static final Set<Character> VOWELS
    = Set.of('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U');

  public boolean halvesAreAlike(String s) {
    int cnt = 0, n = s.length() >> 1;
    for (int i = 0; i < n; ++i) {
      cnt += VOWELS.contains(s.charAt(i)) ? 1 : 0;
      cnt -= VOWELS.contains(s.charAt(i + n)) ? 1 : 0;
    }
    return cnt == 0;
  }
}
class Solution {
public:
  bool halvesAreAlike(string s) {
    unordered_set<char> vowels = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'};
    int cnt = 0, n = s.size() / 2;
    for (int i = 0; i < n; ++i) {
      cnt += vowels.count(s[i]);
      cnt -= vowels.count(s[i + n]);
    }
    return cnt == 0;
  }
};
func halvesAreAlike(s string) bool {
  vowels := map[byte]bool{}
  for _, c := range "aeiouAEIOU" {
    vowels[byte(c)] = true
  }
  cnt, n := 0, len(s)>>1
  for i := 0; i < n; i++ {
    if vowels[s[i]] {
      cnt++
    }
    if vowels[s[i+n]] {
      cnt--
    }
  }
  return cnt == 0
}
function halvesAreAlike(s: string): boolean {
  const set = new Set(['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']);
  const n = s.length >> 1;
  let count = 0;
  for (let i = 0; i < n; i++) {
    set.has(s[i]) && count++;
    set.has(s[n + i]) && count--;
  }
  return count === 0;
}
use std::collections::HashSet;
impl Solution {
  pub fn halves_are_alike(s: String) -> bool {
    let set: HashSet<&u8> = [b'a', b'e', b'i', b'o', b'u', b'A', b'E', b'I', b'O', b'U']
      .into_iter()
      .collect();
    let s = s.as_bytes();
    let n = s.len() >> 1;
    let mut count = 0;
    for i in 0..n {
      if set.contains(&s[i]) {
        count += 1;
      }
      if set.contains(&s[n + i]) {
        count -= 1;
      }
    }
    count == 0
  }
}
/**
 * @param {string} s
 * @return {boolean}
 */
var halvesAreAlike = function (s) {
  const str = 'aeiouAEIOU';
  let cnt = 0;
  for (let i = 0; i < s.length / 2; i++) {
    if (str.indexOf(s[i]) > -1) cnt++;
    if (str.indexOf(s[s.length - 1 - i]) > -1) cnt--;
  }
  return cnt === 0;
};
class Solution {
  /**
   * @param String $s
   * @return Boolean
   */
  function halvesAreAlike($s) {
    $cnt = 0;
    for ($i = 0; $i < strlen($s) / 2; $i++) {
      if (strpos('aeiouAEIOU', $s[$i]) !== false) {
        $cnt++;
      }
      if (strpos('aeiouAEIOU', $s[strlen($s) / 2 + $i]) !== false) {
        $cnt--;
      }
    }
    return $cnt == 0;
  }
}

Solution 2

class Solution:
  def halvesAreAlike(self, s: str) -> bool:
    vowels = set('aeiouAEIOU')
    a, b = s[: len(s) >> 1], s[len(s) >> 1 :]
    return sum(c in vowels for c in a) == sum(c in vowels for c in b)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文