Easy Pronunciation---Difficulty: 1000---Python Solution

Problem: Easy Pronunciation

Problem Statement:

We say that a word is hard to pronounce if it contains 4 or more consonants in a row. Otherwise, it is considered easy to pronounce. Your task is to determine if a given word is easy to pronounce or hard to pronounce based on this rule.

Input:

  • The first line contains an integer T representing the number of test cases.
  • For each test case:
    • The first line contains an integer N representing the length of the string S (this value is not necessary for solving the problem but is given as part of the input).
    • The second line contains a string S consisting of N lowercase Latin characters.

Output:

  • For each test case, output YES if the word is easy to pronounce (i.e., it does not have 4 consecutive consonants).
  • Output NO if the word is hard to pronounce (i.e., it has 4 or more consecutive consonants).

Approach:

To solve this problem, we need to determine if there are 4 or more consecutive consonants in each string. Here’s the step-by-step approach:

  1. Vowels and Consonants: We know that vowels are the characters a, e, i, o, u, and all other lowercase letters are consonants.
  2. Iterate through the string: For each test case:
  • We will loop through the string and keep a count of consecutive consonants.
  • If we find a vowel, we reset the counter because the sequence of consonants breaks.
  • If we find 4 or more consecutive consonants at any point, we can immediately stop and print “NO” for that test case.
  • If we finish processing the string and don’t find 4 consecutive consonants, we print “YES”.

Solution Code:

python

Copy

vowels = set('aeiou')  # Set of vowels for easy lookup
T = int(input())  # Number of test cases

# Process each test case
for _ in range(T):
    N = int(input())  # Length of the string (not used directly)
    S = input()  # The string itself
    
    consecutive_consonants = 0  # Initialize counter for consecutive consonants
    result = "YES"  # Assume the word is easy to pronounce initially
    
    # Iterate through each character in the string
    for char in S:
        if char not in vowels:  # If the character is a consonant
            consecutive_consonants += 1
            if consecutive_consonants >= 4:  # If we have 4 or more consonants in a row
                result = "NO"  # Set result to "NO"
                break  # No need to check further, we already know the answer
        else:
            consecutive_consonants = 0  # Reset the counter when we encounter a vowel
    
    # Print the result for the current test case
    print(result)