1704. Determine if String Halves Are Alike

Determine if String Halves Are Alike

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.



Approach) Using Set

/**
 * @param {string} s
 * @return {boolean}
 */
const halvesAreAlike = (
  s,
  vowels = new Set(['A', 'E', 'I', 'O', 'U', 'a', 'e', 'i', 'o', 'u']),
) =>
  ![...s].reduce(
    (acc, curr, idx) =>
      acc + +(vowels.has(curr) ? (idx < s.length / 2 ? 1 : -1) : 0),
    0,
  );
And here is the boring multi-line version everybody craves.

/**
 * @param {string} s
 * @return {boolean}
 */
const halvesAreAlike = s => {
  let sum = 0;
  const vowels = new Set(['A', 'E', 'I', 'O', 'U', 'a', 'e', 'i', 'o', 'u']);
  for (let i = 0; i < s.length / 2; i++)
    sum += vowels.has(s[i]) - vowels.has(s[s.length - 1 - i]);
  return !sum;
};
Approach) One Liner 

It is not readable it is terse

var halvesAreAlike =s=> s.split("").slice(0, s.length/2).reduce((v,e)=>"AEIOU".includes(e.toUpperCase())?v+=1:v, 0) === s.split("").slice(s.length/2).reduce((v,e)=>"AEIOU".includes(e.toUpperCase())?v+=1:v, 0) 
Approach) With Fixed Length Array

const halvesAreAlike = s => {
  const code = new Uint8Array(123);
  const target = 'aeiouAEIOU';
  let a = 0, b = 0;
  for (let i = 0; i < target.length; ++i) {
    code[target.charCodeAt(i)] = 1;
  }
  for (let left = 0, right = s.length - 1; left < right; ++left, --right) {
    a += code[s.charCodeAt(left)];
    b += code[s.charCodeAt(right)];
  }
  return a === b;
};

Approach) Set + Mid

var halvesAreAlike = function(s) {
    let ar = new Set(['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'])
    
    let mid = s.length / 2
    let countL=0
    for (let word of s) {
        if (ar.has(word)) {
           countL++ 
        }
        mid--
        if (mid === 0){
            countL *= -1
        }
        
    }
    
    return countL === 0;
};

Conclusion

That’s all folks! In this post, we solved LeetCode problem 1704. Determine if String Halves Are Alike

I hope you have enjoyed this post. Feel free to share your thoughts on this.

You can find the complete source code on my GitHub repository. If you like what you learn. feel free to fork 🔪 and star ⭐ it.


In this blog, I have tried to collect & present the most important points to consider when improving Data structure and logic, feel free to add, edit, comment, or ask. For more information please reach me here
Happy coding!

Comments

Popular Post