Convert Number Words to Digits
You are given a string s consisting only of lowercase English letters. The string may contain valid English words representing digits from 0 to 9, concatenated without spaces.
Your task is to scan the string from left to right and convert every valid number word into its corresponding digit.
While traversing the string:
- You must parse the string strictly from left to right.
- At every index, check if a valid number word starts exactly at that position.
- If matched, append the digit and move forward by that word’s full length.
- Otherwise, skip exactly one character and continue.
- Move the pointer forward by the length of that word.
Return the final string of digits obtained after processing the entire string.
If no valid number words are found, return an empty string.
Example 1:
Input: s = "zeroonetwo"
Output: "012"
Explanation:
- "zero" → 0
- "one" → 1
- "two" → 2
All words are valid and extracted sequentially.
Example 2:
Input: s = "abcthreexfive"
Output: "35"
Explanation:
Characters "a", "b", "c" are skipped.
- "three" → 3
- "x" is skipped.
- "five" → 5
Final result: "35".
Now Your Turn!
Pick the correct output for the given inputInput: s = "threeight"
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 1 ≤ s.length ≤ 105
- s contains only lowercase English letters ('a' to 'z')
- Valid number words are exactly: "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"