Skip to main content

Command Palette

Search for a command to run...

Determine if String Halves Are Alike

Published
2 min read
Determine if String Halves Are Alike
G

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.

I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.

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.

LeetCode Problem: Link | Click Here

import java.util.ArrayList;

class Solution {
    public boolean halvesAreAlike(String s) {
        // Calculate the index to split the string into two halves
        int splitStr = s.length() / 2;
        boolean result = false;

        // Create an ArrayList to store vowels
        ArrayList<Character> vowels = new ArrayList<>();
        vowels.add('a'); vowels.add('e'); vowels.add('i'); vowels.add('o'); vowels.add('u');
        vowels.add('A'); vowels.add('E'); vowels.add('I'); vowels.add('O'); vowels.add('U');

        // Extract the first half of the string
        String a = s.substring(0, splitStr);
        // Extract the second half of the string
        String b = s.substring(splitStr);

        // Count the number of vowels in each half
        int aa = vowelsCount(a, vowels);
        int bb = vowelsCount(b, vowels);

        // Check if the counts of vowels in both halves are equal
        if (aa == bb) {
            result = true;
        }

        return result;
    }

    // Method to count the number of vowels in a given string
    public int vowelsCount(String s, ArrayList<Character> vowels) {
        int count = 0;
        // Iterate through the characters of the string
        for (int i = 0; i < s.length(); i++) {
            // Check if the character is a vowel
            if (vowels.contains(s.charAt(i))) {
                count++;
            }
        }
        return count;
    }
}

More from this blog

S

Software and Performance Testing Insights

462 posts

Results-Driven Agile QA Specialist | Expert in Mobile & Web Testing | Proficient in Test Planning, Execution, and Root Cause Analysis.

Determine if String Halves Are Alike