Back to blog
Mar 09, 2026
8 min read

Check if Numbers Are Ascending in a Sentence

Check if all numbers in a sentence are in strictly ascending order from left to right.

Difficulty: Easy | Acceptance: 73.10% | Paid: No Topics: String

A sentence is a list of tokens separated by a single space with no leading or trailing spaces. Each token consists of English letters and/or digits. A token is numeric if it consists only of digits. Return true if all numeric tokens in the sentence are in strictly ascending order, or false otherwise.

Examples

Example 1:

Input: s = "1 box has 3 blue 4 red 6 green and 12 yellow marbles"
Output: true
Explanation: The numbers in s are: 1, 3, 4, 6, 12. They are strictly increasing.

Example 2:

Input: s = "hello world 5 x 5"
Output: false
Explanation: The numbers in s are: 5, 5. They are not strictly increasing.

Example 3:

Input: s = "sunset is at 7 51 pm overnight lows will be in the 50 60 s"
Output: false
Explanation: The numbers in s are: 7, 51, 50, 60. They are not strictly increasing.

Constraints

- 3 <= s.length <= 200
- s consists of lowercase English letters, spaces, and digits from 0 to 9, inclusive.
- The number of tokens in s is between 2 and 100, inclusive.
- The tokens in s are separated by a single space.
- There are at least two numbers in s.
- Each number in s is a positive number less than 100, with no leading zeros.
- s contains no leading or trailing spaces.

Table of Contents


Simple Iteration

Intuition Split the sentence by spaces and iterate through each token, checking if it’s numeric and comparing it with the previous number.

Steps

  • Split the sentence by spaces
  • Initialize previous number to -1
  • For each token, check if it’s numeric
  • If numeric, compare with previous number
  • Return false if current number is not greater than previous
  • Update previous number and continue
python
class Solution:
    def areNumbersAscending(self, s: str) -> bool:
        tokens = s.split()
        prev = -1
        for token in tokens:
            if token.isdigit():
                curr = int(token)
                if curr &lt;= prev:
                    return False
                prev = curr
        return True

Complexity

  • Time: O(n) where n is the length of the string
  • Space: O(n) for storing the tokens
  • Notes: Simple and readable approach

Regular Expression

Intuition Use regular expression to extract all numbers from the sentence, then check if they form a strictly increasing sequence.

Steps

  • Use regex to find all numeric substrings
  • Convert them to integers
  • Iterate through the numbers and check if each is greater than the previous
python
import re

class Solution:
    def areNumbersAscending(self, s: str) -> bool:
        numbers = list(map(int, re.findall(r'\d+', s)))
        for i in range(1, len(numbers)):
            if numbers[i] &lt;= numbers[i - 1]:
                return False
        return True

Complexity

  • Time: O(n) where n is the length of the string
  • Space: O(n) for storing the numbers
  • Notes: Concise but requires regex knowledge

Character-by-Character Parsing

Intuition Parse the string character by character, building numbers digit by digit, and check ascending order without splitting or using regex.

Steps

  • Initialize previous number to -1 and current number to 0
  • Flag to track if we’re currently parsing a number
  • Iterate through each character
  • If digit, build the current number
  • If space and we were parsing a number, compare with previous
  • Return false if not strictly increasing
python
class Solution:
    def areNumbersAscending(self, s: str) -> bool:
        prev = -1
        curr = 0
        in_number = False
        
        for ch in s:
            if ch.isdigit():
                curr = curr * 10 + int(ch)
                in_number = True
            elif in_number:
                if curr &lt;= prev:
                    return False
                prev = curr
                curr = 0
                in_number = False
        
        if in_number and curr &lt;= prev:
            return False
        
        return True

Complexity

  • Time: O(n) where n is the length of the string
  • Space: O(1) constant extra space
  • Notes: Most space-efficient approach