Java: 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'.

import java.util.Scanner;

public class CF {

    public static boolean isVowel(Character ch){

        if(ch.equals('a') || ch.equals('e') || ch.equals('i') || ch.equals('o') || ch.equals('u'))
            return true;
        else
            return false;
    }
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        String str1 = sc.nextLine();

        int cnt1=0;
        for(int i=0;i<str1.length();i++){

            if(isVowel(str1.charAt(i))){
                cnt1++;
            }
        }

        String str2 = sc.nextLine();

        int cnt2=0;
        for(int i=0;i<str2.length();i++){

            if(isVowel(str2.charAt(i))){
                cnt2++;
            }
        }

        String str3 = sc.nextLine();

        int cnt3=0;
        for(int i=0;i<str3.length();i++){

            if(isVowel(str3.charAt(i))){
                cnt3++;
            }
        }
        if(cnt1==5 && cnt2==7 && cnt3==5)
            System.out.println("YES");
        else
            System.out.println("NO");
    }
}

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!