Advertisement

Number of Valid Words in a Sentence - LeetCode 2047 Solution

Number of Valid Words in a Sentence - Complete Solution Guide

Number of Valid Words in a Sentence is LeetCode problem 2047, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

A sentence consists of lowercase letters ( 'a' to 'z' ), digits ( '0' to '9' ), hyphens ( '-' ), punctuation marks ( '!' , '.' , and ',' ), and spaces ( ' ' ) only. Each sentence can be broken down into one or more tokens separated by one or more spaces ' ' . A token is a valid word if all three of the following are true: It only contains lowercase letters, hyphens, and/or punctuation ( no digits). There is at most one hyphen '-' . If present, it must be surrounded by lowercase characters ( "a-b

Detailed Explanation

The problem asks you to count the number of valid words in a given sentence. A valid word is defined by three rules: 1. **No digits:** The word must contain only lowercase letters, hyphens, and punctuation marks (!, ., ,). 2. **At most one hyphen:** If a hyphen exists, it must be surrounded by lowercase letters (e.g., "a-b" is valid, but "-ab" and "ab-" are not). 3. **At most one punctuation mark:** If a punctuation mark is present, it must be at the end of the word (e.g., "ab," is valid, but "a!b" is not). The input is a string representing the sentence, and the output is an integer representing the number of valid words.

Solution Approach

The provided solutions use a common approach. They first split the input sentence into tokens using whitespace as the delimiter. Then, for each token, they iterate through its characters, checking for violations of the three rules. If a violation is found, the word is deemed invalid. A counter tracks the number of valid words, which is returned as the final result. The `isValid` helper function (in Java and C++) encapsulates the word validation logic for better code organization and readability.

Step-by-Step Algorithm

  1. Split the input sentence into tokens using whitespace as the delimiter.
  2. Iterate through each token:
  3. - Initialize counters for hyphens and punctuation marks.
  4. - Iterate through the characters of the current token:
  5. - Check for digits; if found, mark the token as invalid.
  6. - Check for hyphens; if found, increment the hyphen counter and check for rule violations (multiple hyphens, hyphen at the beginning or end, or hyphen not surrounded by lowercase letters).
  7. - Check for punctuation marks; if found, increment the punctuation counter and check for rule violations (multiple punctuation marks, punctuation mark not at the end).
  8. - If any rule violation is detected, break the inner loop and mark the token as invalid.
  9. - If no rule violation is detected, increment the count of valid words.
  10. Return the total count of valid words.

Key Insights

  • The sentence needs to be split into individual words (tokens) before processing each word.
  • A state machine or iterative approach can efficiently check the validity of each word based on the rules.
  • Proper handling of edge cases like empty tokens, words with multiple hyphens or punctuation, and words starting or ending with hyphens is crucial.

Complexity Analysis

Time Complexity: O(N)

Space Complexity: O(N)

Topics

This problem involves: String.

Companies

Asked at: Cisco.