Skip to content
Related Articles

Related Articles

Calculate the difficulty of a sentence

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 14 Jul, 2022
Improve Article
Save Article

Calculate difficulty of a given sentence. Here a Word is considered hard if it has 4 consecutive consonants or number of consonants is more than number of vowels. Else word is easy. Difficulty of sentence is defined as 5*(number of hard words) + 3*(number of easy words).

Examples: 

Input : str = "Difficulty of sentence"
Output : 13
Hard words = 2(Difficulty and sentence)
Easy words = 1(of)
So, answer is 5*2+3*1 = 13

Asked in : Microsoft

Recommended Practice

Implementation: 
Start traversing the string and perform following steps:

  • Increment vowels count if current character is vowel and set consecutive consonants count=0.
  • Else increment consonants count, also increment consecutive consonants count.
  • Check if consecutive consonants become 4, then-current word is hard, so increment its count and move to the next word. Reset all counts to 0.
  • Else check if a word is completed and count of consonants is greater than count of vowels, 
    then it is a hard word else easy word. Reset all counts to 0.

Implementation:

C++




// C++ program to find difficulty of a sentence
#include <iostream>
using namespace std;
 
// Utility function to check character is vowel
// or not
bool isVowel(char ch)
{
    return ( ch == 'a' || ch == 'e' ||
             ch == 'i' || ch == 'o' ||
             ch == 'u');
}
 
// Function to calculate difficulty
int calcDiff(string str)
{
 
    int count_vowels = 0, count_conso = 0;
    int hard_words = 0, easy_words = 0;
    int consec_conso = 0;
 
    // Start traversing the string
    for (int i = 0; i < str.length(); i++)
    {
        // Check if current character is vowel
        // or consonant
        if (str[i] != ' ' && isVowel(tolower(str[i])))
        {
            // Increment if vowel
            count_vowels++;
            consec_conso = 0;
        }
 
        // Increment counter for consonant
        // also maintain a separate counter for
        // counting consecutive consonants
        else if (str[i]!= ' ')
        {
            count_conso++;
            consec_conso++;
        }
 
        // If we get 4 consecutive consonants
        // then it is a hard word
        if (consec_conso == 4)
        {
            hard_words++;
 
            // Move to the next word
            while (i < str.length() && str[i]!= ' ')
                i++;
 
            // Reset all counts
            count_conso = 0;
            count_vowels = 0;
            consec_conso = 0;
        }
 
        else if ( i < str.length() &&
                  (str[i] == ' ' || i == str.length()-1))
        {
            // Increment hard_words, if no. of consonants are
            // higher than no. of vowels, otherwise increment
            // count_vowels
            count_conso > count_vowels ? hard_words++
                                       : easy_words++;
 
            // Reset all counts
            count_conso = 0;
            count_vowels = 0;
            consec_conso = 0;
        }
    }
 
    // Return difficulty of sentence
    return 5 * hard_words + 3 * easy_words;
}
 
// Drivers code
int main()
{
    string str = "I am a geek";
    string str2 = "We are geeks";
    cout << calcDiff(str) << endl;
    cout << calcDiff(str2) << endl;
 
    return 0;
}

Java




// Java program to find difficulty of a sentence
 
class GFG
{
    // Utility method to check character is vowel
    // or not
    static boolean isVowel(char ch)
    {
        return ( ch == 'a' || ch == 'e' ||
                 ch == 'i' || ch == 'o' ||
                 ch == 'u');
    }
      
    // Method to calculate difficulty
    static int calcDiff(String str)
    {
      
        int count_vowels = 0, count_conso = 0;
        int hard_words = 0, easy_words = 0;
        int consec_conso = 0;
      
        // Start traversing the string
        for (int i = 0; i < str.length(); i++)
        {
            // Check if current character is vowel
            // or consonant
            if (str.charAt(i) != ' ' && isVowel(Character.toLowerCase(str.charAt(i))))
            {
                // Increment if vowel
                count_vowels++;
                consec_conso = 0;
            }
      
            // Increment counter for consonant
            // also maintain a separate counter for
            // counting consecutive consonants
            else if (str.charAt(i)!= ' ')
            {
                count_conso++;
                consec_conso++;
            }
      
            // If we get 4 consecutive consonants
            // then it is a hard word
            if (consec_conso == 4)
            {
                hard_words++;
      
                // Move to the next word
                while (i < str.length() && str.charAt(i)!= ' ')
                    i++;
      
                // Reset all counts
                count_conso = 0;
                count_vowels = 0;
                consec_conso = 0;
            }
      
            else if ( i < str.length() &&
                      (str.charAt(i) == ' ' || i == str.length()-1))
            {
                // Increment hard_words, if no. of consonants are
                // higher than no. of vowels, otherwise increment
                // count_vowels
                if(count_conso > count_vowels)
                    hard_words++;
                else
                    easy_words++;
      
                // Reset all counts
                count_conso = 0;
                count_vowels = 0;
                consec_conso = 0;
            }
        }
      
        // Return difficulty of sentence
        return 5 * hard_words + 3 * easy_words;
    }
     
    // Driver method
    public static void main (String[] args)
    {
        String str = "I am a geek";
        String str2 = "We are geeks";
        System.out.println(calcDiff(str));
        System.out.println(calcDiff(str2));
    }
}

Python 3




# Python3 program to find difficulty
# of a sentence
 
# Utility function to check character
# is vowel or not
def isVowel(ch):
    return (ch == 'a' or ch == 'e' or
            ch == 'i' or ch == 'o' or
            ch == 'u')
 
# Function to calculate difficulty
def calcDiff(str):
    str = str.lower()
    count_vowels = 0
    count_conso = 0
    consec_conso = 0
    hard_words = 0
    easy_words = 0
 
    # Start traversing the string
    for i in range(0, len(str)):
         
        # Check if current character is
        # vowel or consonant
        if(str[i]!= " " and isVowel(str[i])):
             
            # Increment if vowel
            count_vowels += 1
            consec_conso = 0
             
        # Increment counter for consonant
        # also maintain a separate counter for
        # counting consecutive consonants
        elif(str[i] != " "):
            count_conso += 1
            consec_conso += 1
 
        # If we get 4 consecutive consonants
        # then it is a hard word
        if(consec_conso == 4):
            hrad_words += 1
 
            # Move to the next word
            while(i < len(str) and str[i] != " "):
                i += 1
                 
            # Reset all counts
            count_conso = 0
            count_vowels = 0
            consec_conso = 0
        elif(i < len(str) and (str[i] == ' ' or
                          i == len(str) - 1)):
                               
            # Increment hard_words, if no. of
            # consonants are higher than no. of
            # vowels, otherwise increment count_vowels
            if(count_conso > count_vowels):
                hard_words += 1
            else:
                easy_words += 1
 
            # Reset all counts
            count_conso = 0
            count_vowels = 0
            consec_conso = 0
             
    # Return difficulty of sentence    
    return (5 * hard_words + 3 * easy_words)
     
# Driver Code
if __name__=="__main__":
    str = "I am a geek"
    str2 = "We are geeks"
    print(calcDiff(str))
    print(calcDiff(str2))
 
# This code is contributed
# by Sairahul Jella

C#




// C# program to find difficulty
// of a sentence
using System;
     
public class GFG {
     
    // Utility method to check character
    // is vowel or not
    static bool isVowel(char ch)
    {
        return (ch == 'a' || ch == 'e' ||
                ch == 'i' || ch == 'o' ||
                ch == 'u');
    }
     
    // Method to calculate difficulty
    static int calcDiff(string str)
    {
        int count_vowels = 0, count_conso = 0;
        int hard_words = 0, easy_words = 0;
        int consec_conso = 0;
     
        // Start traversing the string
        for (int i = 0; i < str.Length; i++)
        {
             
            // Check if current character
            // is vowel or consonant
            if (str[i] != ' ' &&
                isVowel(char.ToLower( str[i])))
            {
                // Increment if vowel
                count_vowels++;
                consec_conso = 0;
            }
     
            // Increment counter for consonant
            // also maintain a separate counter for
            // counting consecutive consonants
            else if (str[i]!= ' ')
            {
                count_conso++;
                consec_conso++;
            }
     
            // If we get 4 consecutive consonants
            // then it is a hard word
            if (consec_conso == 4)
            {
                hard_words++;
     
                // Move to the next word
                while (i < str.Length && str[i]!= ' ')
                    i++;
     
                // Reset all counts
                count_conso = 0;
                count_vowels = 0;
                consec_conso = 0;
            }
     
            else if ( i < str.Length &&
                    (str[i] == ' ' || i == str.Length-1))
            {
                 
                // Increment hard_words, if no. of
                // consonants are higher than no.
                // of vowels, otherwise increment
                // count_vowels
                if(count_conso > count_vowels)
                    hard_words++;
                else
                    easy_words++;
     
                // Reset all counts
                count_conso = 0;
                count_vowels = 0;
                consec_conso = 0;
            }
        }
     
        // Return difficulty of sentence
        return 5 * hard_words + 3 * easy_words;
    }
     
    // Driver code
    public static void Main ()
    {
        string str = "I am a geek";
        string str2 = "We are geeks";
        Console.WriteLine(calcDiff(str));
        Console.WriteLine(calcDiff(str2));
    }
}
 
// This code is contributed by Sam007.

Javascript




<script>
 
    // JavaScript program to find
    // difficulty of a sentence
     
    // Utility method to check character
    // is vowel or not
    function isVowel(ch)
    {
        return (ch == 'a' || ch == 'e' ||
                ch == 'i' || ch == 'o' ||
                ch == 'u');
    }
       
    // Method to calculate difficulty
    function calcDiff(str)
    {
        let count_vowels = 0, count_conso = 0;
        let hard_words = 0, easy_words = 0;
        let consec_conso = 0;
       
        // Start traversing the string
        for (let i = 0; i < str.length; i++)
        {
               
            // Check if current character
            // is vowel or consonant
            if (str[i] != ' ' &&
                isVowel(str[i].toLowerCase()))
            {
                // Increment if vowel
                count_vowels++;
                consec_conso = 0;
            }
       
            // Increment counter for consonant
            // also maintain a separate counter for
            // counting consecutive consonants
            else if (str[i]!= ' ')
            {
                count_conso++;
                consec_conso++;
            }
       
            // If we get 4 consecutive consonants
            // then it is a hard word
            if (consec_conso == 4)
            {
                hard_words++;
       
                // Move to the next word
                while (i < str.length && str[i]!= ' ')
                    i++;
       
                // Reset all counts
                count_conso = 0;
                count_vowels = 0;
                consec_conso = 0;
            }
       
            else if ( i < str.length &&
                    (str[i] == ' ' || i == str.length-1))
            {
                   
                // Increment hard_words, if no. of
                // consonants are higher than no.
                // of vowels, otherwise increment
                // count_vowels
                if(count_conso > count_vowels)
                    hard_words++;
                else
                    easy_words++;
       
                // Reset all counts
                count_conso = 0;
                count_vowels = 0;
                consec_conso = 0;
            }
        }
       
        // Return difficulty of sentence
        return 5 * hard_words + 3 * easy_words;
    }
     
    let str = "I am a geek";
    let str2 = "We are geeks";
    document.write(calcDiff(str) + "</br>");
    document.write(calcDiff(str2));
     
</script>

Output

12
11

Time complexity : O(n) 
Auxiliary Space : O(1)

This article is contributed by Sahil Chhabra. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. 


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!