Quick actions

cmd+k|ctrl+k

Navigation

Languages

is_balanced

Snippet info

Language

Python

Visibility

public

Author

hospino11

Created

2026-07-06T16:01:43.558488Z

Updated

2026-07-06T16:47:20.314445Z

def is_balanced(s):
    vowels = ["a", "e", "i", "o", "u"]
    firstHalfCounter = secondHalfCounter = 0
    stringToCheck = s.casefold()
    length = len(s)
    isOdd = length % 2 > 0
    mid = length // 2
    firstHalf = stringToCheck[:mid]
    secondHalf = stringToCheck[mid:]
    if isOdd:
        secondHalf = stringToCheck[mid+1:]

    for i in range(len(firstHalf)):
        if firstHalf[i] in vowels:
            firstHalfCounter += 1

        if secondHalf[i] in vowels:
            secondHalfCounter += 1

    print(f"First half counter {firstHalfCounter} with {firstHalf}, second half counter {secondHalfCounter} with {secondHalf}")

    return firstHalfCounter == secondHalfCounter

result = is_balanced("racecar")

print(f"Is racecar a balanced vowels string? {result}")

result = is_balanced("Lorem Ipsum")

print(f"Is Lorem Ipsum a balanced vowels string? {result}")

"""
Given a string, determine whether the number of vowels in the first half of the string is equal to the number of vowels in the second half.

"""
INFO