Problem Description
Given a sentence where each token is either a positive number (with no leading zeros) or a lowercase word, determine if all the numbers in the sentence are strictly increasing from left to right.
Key Insights
- Split the sentence into tokens using space as the delimiter.
- Identify if a token is a number by checking if the token consists entirely of digits.
- Convert numeric tokens into integers and compare to ensure that each number is strictly greater than the previous one.
- Non-numeric tokens (words) can be ignored during the comparison.
Space and Time Complexity
Time Complexity: O(n) where n is the number of tokens (or the length of the sentence), since each token is processed once. Space Complexity: O(n) due to the storage of tokens from the sentence split.
Solution
We split the sentence into individual tokens. Then, we iterate over these tokens, ignoring the words and converting the numeric tokens to integers. As we iterate, we maintain a variable to store the previous number encountered. For each numeric token, if its integer value is not strictly greater than the previous number, we return false. If all numeric tokens pass this check, we return true. This approach makes a single pass through the list of tokens and uses simple integer comparisons.