Determine If String Halves Are Alike

Solution For Determine If String Halves Are Alike

Problem Statement:

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:

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

Solution:

In order to solve this problem, we need to count the number of vowels in both halves of the given string. Since the number of vowels in the string is not fixed, we cannot use an array to store the count of vowels. Therefore, we will use two separate count variables to keep track of the number of vowels in both halves of the string.

In the first step, we will split the given string into two halves a and b using the substring method in python. We can use the len() function to calculate the length of the string and then divide it by 2 to get the middle position of the string.

Then we will create a function called count_vowels to count the number of vowels in a given string. We will iterate through the string character by character, and if we encounter a vowel, we will increase the count of vowels.

Finally, in the main function, we will call the count_vowels function on both halves of the string, and if the count of vowels in both halves is the same, we will return True. Otherwise, we will return False.

Here is the Python code for the solution:

def count_vowels(s):
count = 0
vowels = set(‘aeiouAEIOU’)
for char in s:
if char in vowels:
count += 1
return count

def halvesAreAlike(s: str) -> bool:
n = len(s)
a = s[:n//2] b = s[n//2:] return count_vowels(a) == count_vowels(b)

Time Complexity: O(N), where N is the length of the string s
Space Complexity: O(1), as we are not using any extra space

This solution is efficient both in terms of time and space complexity. If you submit this solution on LeetCode, it will pass all test cases.

Step by Step Implementation For Determine If String Halves Are Alike

public boolean halvesAreAlike(String s) {
    
    //check if length of string is even
    if(s.length() % 2 != 0){
        return false;
    }
    
    //get length of each half
    int half = s.length() / 2;
    
    //get substrings for each half
    String firstHalf = s.substring(0, half);
    String secondHalf = s.substring(half, s.length());
    
    //initialize counts for each character in the alphabet for each half
    int[] firstHalfCounts = new int[26];
    int[] secondHalfCounts = new int[26];
    
    //count each character in each half
    for(int i = 0; i < firstHalf.length(); i++){
        char c = firstHalf.charAt(i);
        firstHalfCounts[c - 'a']++;
    }
    for(int i = 0; i < secondHalf.length(); i++){
        char c = secondHalf.charAt(i);
        secondHalfCounts[c - 'a']++;
    }
    
    //compare counts
    for(int i = 0; i < 26; i++){
        if(firstHalfCounts[i] != secondHalfCounts[i]){
            return false;
        }
    }
    
    return true;
}
def halvesAreAlike(s): 

# initialize two counters, one for each half of the string 
left = 0
right = 0

# iterate through the string, incrementing the appropriate counter for each character 
for i in range(len(s)): 
if i < len(s) / 2: 
left += ord(s[i]) 
else: 
right += ord(s[i]) 

# compare the two counters and return the appropriate boolean value 
return left == right
function halvesAreAlike(s) {
  // your code here
}
bool halvesAreAlike(string s) 
{ 
    int n = s.length(); 
  
    // Length of the string must be even 
    if (n%2 != 0) 
        return false; 
  
    // Divide the string into two halves 
    string s1 = s.substr(0, n/2); 
    string s2 = s.substr(n/2); 
  
    // Sort both the halves 
    sort(s1.begin(), s1.end()); 
    sort(s2.begin(), s2.end()); 
  
    // Compare both the halves 
    for (int i = 0; i < n/2; i++) 
        if (s1[i] != s2[i]) 
            return false; 
  
    return true; 
}
public bool halvesAreAlike(string s) {
    //check if length is even
    if(s.Length % 2 != 0) {
        return false;
    }
    
    int half = s.Length / 2;
    
    //get counts for first and second half
    Dictionary firstHalfCounts = new Dictionary();
    Dictionary secondHalfCounts = new Dictionary();
    
    for(int i = 0; i < half; i++) {
        char c = s[i];
        if(!firstHalfCounts.ContainsKey(c)) {
            firstHalfCounts.Add(c, 1);
        }
        else {
            firstHalfCounts[c]++;
        }
    }
    
    for(int i = half; i < s.Length; i++) {
        char c = s[i];
        if(!secondHalfCounts.ContainsKey(c)) {
            secondHalfCounts.Add(c, 1);
        }
        else {
            secondHalfCounts[c]++;
        }
    }
    
    //compare counts
    foreach(var kvp in firstHalfCounts) {
        char c = kvp.Key;
        int count = kvp.Value;
        
        if(!secondHalfCounts.ContainsKey(c) || secondHalfCounts[c] != count) {
            return false;
        }
    }
    
    return true;
}


Scroll to Top
[gravityforms id="5" description="false" titla="false" ajax="true"]