1704. Determine if String Halves Are Alike LeetCode Solution

In this guide, you will get 1704. Determine if String Halves Are Alike LeetCode Solution with the best time and space complexity. The solution to Determine if String Halves Are Alike problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Determine if String Halves Are Alike solution in C++
  4. Determine if String Halves Are Alike solution in Java
  5. Determine if String Halves Are Alike solution in Python
  6. Additional Resources
1704. Determine if String Halves Are Alike LeetCode Solution image

Problem Statement of Determine if String Halves Are Alike

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.

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)
See also  2953. Count Complete Substrings LeetCode Solution

1704. Determine if String Halves Are Alike LeetCode Solution in C++

class Solution {
 public:
  bool halvesAreAlike(string s) {
    const string a = s.substr(0, s.length() / 2);
    const string b = s.substr(s.length() / 2);
    const int aVowelsCount =
        ranges::count_if(a, [this](char c) { return isVowel(c); });
    const int bVowelsCount =
        ranges::count_if(b, [this](char c) { return isVowel(c); });
    return aVowelsCount == bVowelsCount;
  }

 private:
  bool isVowel(char c) {
    static constexpr string_view kVowels = "aeiouAEIOU";
    return kVowels.find(c) != string_view::npos;
  }
};
/* code provided by PROGIEZ */

1704. Determine if String Halves Are Alike LeetCode Solution in Java

class Solution {
  public boolean halvesAreAlike(String s) {
    final String a = s.substring(0, s.length() / 2);
    final String b = s.substring(s.length() / 2);
    final int aVowelsCount = (int) a.chars().filter(c -> isVowel((char) c)).count();
    final int bVowelsCount = (int) b.chars().filter(c -> isVowel((char) c)).count();
    return aVowelsCount == bVowelsCount;
  }

  private boolean isVowel(char c) {
    return "aeiouAEIOU".indexOf(c) != -1;
  }
}
// code provided by PROGIEZ

1704. Determine if String Halves Are Alike LeetCode Solution in Python

class Solution:
  def halvesAreAlike(self, s: str) -> bool:
    kVowels = 'aeiouAEIOU'
    aVowelsCount = sum(c in kVowels for c in s[:len(s) // 2])
    bVowelsCount = sum(c in kVowels for c in s[len(s) // 2:])
    return aVowelsCount == bVowelsCount
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.