Difficulty of sentence

Last Updated : 5 Jun, 2026

Given a sentence as a string s. Calculate difficulty of a given sentence. Difficulty of sentence is defined as 5*(number of hard words) + 3*(number of easy words). A word in the given string is considered hard if it has 4 consecutive consonants or number of consonants are more than number of vowels. Else the word is easy.
Note: uppercase and lowercase characters are same.

Examples: 

Input: s = "Difficulty of sentence"
Output: 13
Explanation: 2 hard words + 1 easy word

Input: s = "I am good"
Output: 9
Explanation: 3 easy words

Try It Yourself
redirect icon

Asked in : Microsoft

Word by Word Analysis - O(n) Time and O(1) Space

A word is "hard" if it has 4 or more consecutive consonants OR consonants count exceeds vowels count. Otherwise it's "easy". Difficulty score = 5*(hard words) + 3*(easy words).

  • Extract words from sentence.
  • For each word, count vowels, consonants, and consecutive consonants.
  • Mark word as hard if consecutive consonants ≥ 4 OR consonants > vowels.
  • Increment hardWords or easyWords accordingly.
  • Return (5 × hardWords) + (3 × easyWords).
C++
#include <bits/stdc++.h>
using namespace std;

// Returns true if the character is a vowel
bool isVowel(char ch)
{
    ch = tolower(ch);

    return ch == 'a' || ch == 'e' || ch == 'i' ||
           ch == 'o' || ch == 'u';
}

int calcDiff(string &s)
{
    stringstream ss(s);
    string word;

    int hardWords = 0;
    int easyWords = 0;

    // Extract one word at a time from the sentence
    while (ss >> word)
    {
        int vowels = 0;
        int consonants = 0;
        int consecutiveConsonants = 0;

        bool isHard = false;

        // Analyze the current word
        for (char ch : word)
        {
            if (isVowel(ch))
            {
                vowels++;

                // Consecutive consonant streak breaks
                consecutiveConsonants = 0;
            }
            else
            {
                consonants++;
                consecutiveConsonants++;

                // Word becomes hard if 4 consonants appear continuously
                if (consecutiveConsonants >= 4)
                {
                    isHard = true;
                }
            }
        }

        // Word is also hard if consonants are more than vowels
        if (consonants > vowels)
        {
            isHard = true;
        }

        // Update counts
        if (isHard)
        {
            hardWords++;
        }
        else
        {
            easyWords++;
        }
    }

    // Calculate final difficulty
    return (5 * hardWords) + (3 * easyWords);
}

int main()
{
    string s = "hello area strength";

    cout << calcDiff(s);

    return 0;
}
Java
import java.util.*;

class GFG {
    // Returns true if the character is a vowel
    static boolean isVowel(char ch) {
        ch = Character.toLowerCase(ch);

        return ch == 'a' || ch == 'e' || ch == 'i' ||
               ch == 'o' || ch == 'u';
    }

    static int calcDiff(String s) {
        String[] words = s.split("\\s+");

        int hardWords = 0;
        int easyWords = 0;

        // Extract one word at a time from the sentence
        for (String word : words) {
            int vowels = 0;
            int consonants = 0;
            int consecutiveConsonants = 0;

            boolean isHard = false;

            // Analyze the current word
            for (int i = 0; i < word.length(); i++) {
                char ch = word.charAt(i);
                
                if (isVowel(ch)) {
                    vowels++;

                    // Consecutive consonant streak breaks
                    consecutiveConsonants = 0;
                } else {
                    consonants++;
                    consecutiveConsonants++;

                    // Word becomes hard if 4 consonants appear continuously
                    if (consecutiveConsonants >= 4) {
                        isHard = true;
                    }
                }
            }

            // Word is also hard if consonants are more than vowels
            if (consonants > vowels) {
                isHard = true;
            }

            // Update counts
            if (isHard) {
                hardWords++;
            } else {
                easyWords++;
            }
        }

        // Calculate final difficulty
        return (5 * hardWords) + (3 * easyWords);
    }

    public static void main(String[] args) {
        String s = "hello area strength";

        System.out.print(calcDiff(s));
    }
}
Python
# Returns true if the character is a vowel
def isVowel(ch):
    ch = ch.lower()
    return ch in ['a', 'e', 'i', 'o', 'u']


def calcDiff(s):
    words = s.split()

    hardWords = 0
    easyWords = 0

    # Extract one word at a time from the sentence
    for word in words:
        vowels = 0
        consonants = 0
        consecutiveConsonants = 0

        isHard = False

        # Analyze the current word
        for ch in word:
            if isVowel(ch):
                vowels += 1

                # Consecutive consonant streak breaks
                consecutiveConsonants = 0
            else:
                consonants += 1
                consecutiveConsonants += 1

                # Word becomes hard if 4 consonants appear continuously
                if consecutiveConsonants >= 4:
                    isHard = True

        # Word is also hard if consonants are more than vowels
        if consonants > vowels:
            isHard = True

        # Update counts
        if isHard:
            hardWords += 1
        else:
            easyWords += 1

    # Calculate final difficulty
    return (5 * hardWords) + (3 * easyWords)


if __name__ == "__main__":
    s = "hello area strength"
    print(calcDiff(s))
C#
using System;

class GFG {
    // Returns true if the character is a vowel
    static bool isVowel(char ch) {
        ch = char.ToLower(ch);

        return ch == 'a' || ch == 'e' || ch == 'i' ||
               ch == 'o' || ch == 'u';
    }

    static int calcDiff(string s) {
        string[] words = s.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

        int hardWords = 0;
        int easyWords = 0;

        // Extract one word at a time from the sentence
        foreach (string word in words) {
            int vowels = 0;
            int consonants = 0;
            int consecutiveConsonants = 0;

            bool isHard = false;

            // Analyze the current word
            foreach (char ch in word) {
                if (isVowel(ch)) {
                    vowels++;

                    // Consecutive consonant streak breaks
                    consecutiveConsonants = 0;
                } else {
                    consonants++;
                    consecutiveConsonants++;

                    // Word becomes hard if 4 consonants appear continuously
                    if (consecutiveConsonants >= 4) {
                        isHard = true;
                    }
                }
            }

            // Word is also hard if consonants are more than vowels
            if (consonants > vowels) {
                isHard = true;
            }

            // Update counts
            if (isHard) {
                hardWords++;
            } else {
                easyWords++;
            }
        }

        // Calculate final difficulty
        return (5 * hardWords) + (3 * easyWords);
    }

    static void Main(string[] args) {
        string s = "hello area strength";

        Console.Write(calcDiff(s));
    }
}
JavaScript
// Returns true if the character is a vowel
function isVowel(ch) {
    ch = ch.toLowerCase();

    return ch === 'a' || ch === 'e' || ch === 'i' ||
           ch === 'o' || ch === 'u';
}

function calcDiff(s) {
    let words = s.split(/\s+/);

    let hardWords = 0;
    let easyWords = 0;

    // Extract one word at a time from the sentence
    for (let word of words) {
        let vowels = 0;
        let consonants = 0;
        let consecutiveConsonants = 0;

        let isHard = false;

        // Analyze the current word
        for (let i = 0; i < word.length; i++) {
            let ch = word[i];
            
            if (isVowel(ch)) {
                vowels++;

                // Consecutive consonant streak breaks
                consecutiveConsonants = 0;
            } else {
                consonants++;
                consecutiveConsonants++;

                // Word becomes hard if 4 consonants appear continuously
                if (consecutiveConsonants >= 4) {
                    isHard = true;
                }
            }
        }

        // Word is also hard if consonants are more than vowels
        if (consonants > vowels) {
            isHard = true;
        }

        // Update counts
        if (isHard) {
            hardWords++;
        } else {
            easyWords++;
        }
    }

    // Calculate final difficulty
    return (5 * hardWords) + (3 * easyWords);
}

// Driver code
let s = "hello area strength";
console.log(calcDiff(s));

Output
13

Single Pass with Space as Delimiter - O(n) Time and O(1) Space

Process the string character by character without extracting individual words. Treat space as word delimiter. Track vowels, consonants, and consecutive consonants for current word. When space is encountered, evaluate word difficulty and reset counters.

  • Append a space at end to process last word naturally.
  • Initialize all counters to zero.
  • For each character: If space: evaluate word, update hard/easy counts, reset all counters. Else: update vowels/consonants, check consecutive consonants ≥ 4.
  • Restore original string by removing appended space.
  • Return (5 × hardWords) + (3 × easyWords).
C++
#include <bits/stdc++.h>
using namespace std;

// Returns true if the character is a vowel
bool isVowel(char ch)
{
    ch = tolower(ch);

    return ch == 'a' || ch == 'e' || ch == 'i' ||
           ch == 'o' || ch == 'u';
}

int calcDiff(string &s)
{
    int hardWords = 0;
    int easyWords = 0;

    int vowels = 0;
    int consonants = 0;
    int consecutiveConsonants = 0;

    bool isHard = false;
    bool hasCharacter = false;

    // Extra space to process the last word
    s.push_back(' ');

    for (char ch : s)
    {
        // End of a word
        if (ch == ' ')
        {
            // Skip empty words caused by multiple spaces
            if (!hasCharacter)
                continue;

            // Hard if consonants are more than vowels
            if (consonants > vowels)
                isHard = true;

            if (isHard)
                hardWords++;
            else
                easyWords++;

            // Reset for next word
            vowels = 0;
            consonants = 0;
            consecutiveConsonants = 0;
            isHard = false;
            hasCharacter = false;
        }
        else
        {
            hasCharacter = true;

            if (isVowel(ch))
            {
                vowels++;
                consecutiveConsonants = 0;
            }
            else
            {
                consonants++;
                consecutiveConsonants++;

                // Hard if there are 4 consecutive consonants
                if (consecutiveConsonants >= 4)
                    isHard = true;
            }
        }
    }

    s.pop_back();

    return (5 * hardWords) + (3 * easyWords);
}

int main()
{
    string s = "hello area strength";

    cout << calcDiff(s);

    return 0;
}
Java
import java.util.*;

class GFG {
    // Returns true if the character is a vowel
    static boolean isVowel(char ch) {
        ch = Character.toLowerCase(ch);

        return ch == 'a' || ch == 'e' || ch == 'i' ||
               ch == 'o' || ch == 'u';
    }

    static int calcDiff(String s) {
        int hardWords = 0;
        int easyWords = 0;

        // Information about the current word
        int vowels = 0;
        int consonants = 0;
        int consecutiveConsonants = 0;

        boolean isHard = false;

        // Extra space helps process the last word naturally
        StringBuilder sb = new StringBuilder(s);
        sb.append(' ');
        String strWithSpace = sb.toString();

        for (int i = 0; i < strWithSpace.length(); i++) {
            char ch = strWithSpace.charAt(i);
            
            // Reached the end of a word
            if (ch == ' ') {
                // Check second hard-word condition
                if (consonants > vowels) {
                    isHard = true;
                }

                // Update counts
                if (isHard) {
                    hardWords++;
                } else {
                    easyWords++;
                }

                // Reset everything for the next word
                vowels = 0;
                consonants = 0;
                consecutiveConsonants = 0;
                isHard = false;
            } else {
                if (isVowel(ch)) {
                    vowels++;

                    // Vowel breaks the consonant sequence
                    consecutiveConsonants = 0;
                } else {
                    consonants++;
                    consecutiveConsonants++;

                    // Word becomes hard if 4 consonants occur continuously
                    if (consecutiveConsonants >= 4) {
                        isHard = true;
                    }
                }
            }
        }

        // Calculate sentence difficulty
        return (5 * hardWords) + (3 * easyWords);
    }

    public static void main(String[] args) {
        String s = "hello area strength";

        System.out.print(calcDiff(s));
    }
}
Python
# Returns true if the character is a vowel
def isVowel(ch):
    ch = ch.lower()

    return ch == 'a' or ch == 'e' or ch == 'i' or ch == 'o' or ch == 'u'


def calcDiff(s):
    hardWords = 0
    easyWords = 0

    vowels = 0
    consonants = 0
    consecutiveConsonants = 0

    isHard = False
    hasCharacter = False

    # Extra space to process the last word
    s = s + ' '

    for ch in s:
        # End of a word
        if ch == ' ':
            # Skip empty words caused by multiple spaces
            if not hasCharacter:
                continue

            # Hard if consonants are more than vowels
            if consonants > vowels:
                isHard = True

            if isHard:
                hardWords += 1
            else:
                easyWords += 1

            # Reset for next word
            vowels = 0
            consonants = 0
            consecutiveConsonants = 0
            isHard = False
            hasCharacter = False
        else:
            hasCharacter = True

            if isVowel(ch):
                vowels += 1
                consecutiveConsonants = 0
            else:
                consonants += 1
                consecutiveConsonants += 1

                # Hard if there are 4 consecutive consonants
                if consecutiveConsonants >= 4:
                    isHard = True

    return (5 * hardWords) + (3 * easyWords)


if __name__ == "__main__":
    s = "hello area strength"
    print(calcDiff(s))
C#
using System;

class GFG {
    // Returns true if the character is a vowel
    static bool isVowel(char ch) {
        ch = char.ToLower(ch);

        return ch == 'a' || ch == 'e' || ch == 'i' ||
               ch == 'o' || ch == 'u';
    }

    static int calcDiff(string s) {
        int hardWords = 0;
        int easyWords = 0;

        int vowels = 0;
        int consonants = 0;
        int consecutiveConsonants = 0;

        bool isHard = false;
        bool hasCharacter = false;

        // Extra space to process the last word
        string strWithSpace = s + " ";

        foreach (char ch in strWithSpace) {
            // End of a word
            if (ch == ' ') {
                // Skip empty words caused by multiple spaces
                if (!hasCharacter)
                    continue;

                // Hard if consonants are more than vowels
                if (consonants > vowels)
                    isHard = true;

                if (isHard)
                    hardWords++;
                else
                    easyWords++;

                // Reset for next word
                vowels = 0;
                consonants = 0;
                consecutiveConsonants = 0;
                isHard = false;
                hasCharacter = false;
            } else {
                hasCharacter = true;

                if (isVowel(ch)) {
                    vowels++;
                    consecutiveConsonants = 0;
                } else {
                    consonants++;
                    consecutiveConsonants++;

                    // Hard if there are 4 consecutive consonants
                    if (consecutiveConsonants >= 4)
                        isHard = true;
                }
            }
        }

        return (5 * hardWords) + (3 * easyWords);
    }

    static void Main(string[] args) {
        string s = "hello area strength";

        Console.Write(calcDiff(s));
    }
}
JavaScript
// Returns true if the character is a vowel
function isVowel(ch) {
    ch = ch.toLowerCase();

    return ch === 'a' || ch === 'e' || ch === 'i' ||
           ch === 'o' || ch === 'u';
}

function calcDiff(s) {
    let hardWords = 0;
    let easyWords = 0;

    let vowels = 0;
    let consonants = 0;
    let consecutiveConsonants = 0;

    let isHard = false;
    let hasCharacter = false;

    // Extra space to process the last word
    let strWithSpace = s + " ";

    for (let i = 0; i < strWithSpace.length; i++) {
        let ch = strWithSpace[i];
        
        // End of a word
        if (ch === ' ') {
            // Skip empty words caused by multiple spaces
            if (!hasCharacter)
                continue;

            // Hard if consonants are more than vowels
            if (consonants > vowels)
                isHard = true;

            if (isHard)
                hardWords++;
            else
                easyWords++;

            // Reset for next word
            vowels = 0;
            consonants = 0;
            consecutiveConsonants = 0;
            isHard = false;
            hasCharacter = false;
        } else {
            hasCharacter = true;

            if (isVowel(ch)) {
                vowels++;
                consecutiveConsonants = 0;
            } else {
                consonants++;
                consecutiveConsonants++;

                // Hard if there are 4 consecutive consonants
                if (consecutiveConsonants >= 4)
                    isHard = true;
            }
        }
    }

    return (5 * hardWords) + (3 * easyWords);
}

// Driver code
let s = "hello area strength";
console.log(calcDiff(s));

Output
13
Comment