C++: 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'.

#include <bits/stdc++.h>
using namespace std;

bool isVowel(char ch){
    
    if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u')
        return true;
    else
        return false;
}

int main(){

    int cnt1=0,cnt2=0,cnt3=0;
    string str1,str2,str3;

    getline(cin,str1);
    for(int i=0;i<str1.size();i++){
        if(isVowel(str1[i])){
            cnt1++;
        }
    }

    getline(cin,str2);
    for(int i=0;i<str2.size();i++){
        if(isVowel(str2[i])){
            cnt2++;
        }
    }

    getline(cin,str3);
    for(int i=0;i<str3.size();i++){
        if(isVowel(str3[i])){
            cnt3++;
        }
    }
    if(cnt1==5 && cnt2==7 && cnt3==5)
        cout<< "YES" << endl;
    else    
        cout << "NO" << endl;
}

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!