Python: Haiku

Thought Process

The problem requires checking if three given lines of text form a Haiku. A Haiku consists of three lines with 5, 7, and 5 syllables respectively. In this problem, syllables are represented by vowels (a, e, i, o, u). We count the number of vowels in each line and check if they match the required pattern. If the counts are 5, 7, and 5, the output is 'YES'; otherwise, it is 'NO'.

def is_vowel(ch):
    return ch in 'aeiou'

def main():
    str1 = input()
    cnt1 = 0
    for ch in str1:
        if is_vowel(ch):
            cnt1 += 1

    str2 = input()
    cnt2 = 0
    for ch in str2:
        if is_vowel(ch):
            cnt2 += 1

    str3 = input()
    cnt3 = 0
    for ch in str3:
        if is_vowel(ch):
            cnt3 += 1

    if cnt1 == 5 and cnt2 == 7 and cnt3 == 5:
        print("YES")
    else:
        print("NO")

if __name__ == "__main__":
    main() 

Code Complexity

Time Complexity: O(n)

The algorithm iterates through each character of the three input strings, making it linear in time complexity, where 'n' is the total number of characters.

Space Complexity: O(1)

The algorithm uses a constant amount of extra space, regardless of the input size.

Code copied!