Check if Numbers Are Ascending in a Sentence

Python solution:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Solution:
    def areNumbersAscending(self, s: str) -> bool:
        words = s.split() # Split the sentence into words
        prev = -1 # Initialize a variable to keep track of the previous number
        for word in words: # Loop through the words
            if word.isdigit(): # If the word is a number
                num = int(word) # Convert it to integer
                if num <= prev: # If this number is not strictly greater than the previous number
                    return False # Then return False
                prev = num # Update the previous number
        return True # If the loop completes without returning False, then the numbers are strictly increasing, so return True

The approach here is to split the sentence into words, then iterate over the words. If a word is a number (which can be determined by the isdigit method), it is compared with the previous number. If it is not strictly greater, we immediately return False, because the numbers are not strictly increasing. If we get through all the words without finding a pair of numbers where the second is not strictly greater than the first, then the numbers are strictly increasing, so we return True.