VOOZH about

URL: https://www.geeksforgeeks.org/dsa/calculate-difficulty-sentence/

⇱ Difficulty of sentence - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

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

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).

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).

Output
13
Comment
Article Tags: