2042. Check if Numbers Are Ascending in a Sentence LeetCode Solution

In this guide, you will get 2042. Check if Numbers Are Ascending in a Sentence LeetCode Solution with the best time and space complexity. The solution to Check if Numbers Are Ascending in a Sentence problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Check if Numbers Are Ascending in a Sentence solution in C++
  4. Check if Numbers Are Ascending in a Sentence solution in Java
  5. Check if Numbers Are Ascending in a Sentence solution in Python
  6. Additional Resources
2042. Check if Numbers Are Ascending in a Sentence LeetCode Solution image

Problem Statement of Check if Numbers Are Ascending in a Sentence

A sentence is a list of tokens separated by a single space with no leading or trailing spaces. Every token is either a positive number consisting of digits 0-9 with no leading zeros, or a word consisting of lowercase English letters.

For example, “a puppy has 2 eyes 4 legs” is a sentence with seven tokens: “2” and “4” are numbers and the other tokens such as “puppy” are words.

Given a string s representing a sentence, you need to check if all the numbers in s are strictly increasing from left to right (i.e., other than the last number, each number is strictly smaller than the number on its right in s).
Return true if so, or false otherwise.

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 from left to right: 1 < 3 < 4 < 6 < 12.

See also  436. Find Right Interval LeetCode Solution

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 low 50 and 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.

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)

2042. Check if Numbers Are Ascending in a Sentence LeetCode Solution in C++

class Solution {
 public:
  bool areNumbersAscending(string s) {
    int prev = 0;
    istringstream iss(s);

    for (string token; iss >> token;)
      if (isdigit(token[0])) {
        const int num = stoi(token);
        if (num <= prev)
          return false;
        prev = num;
      }

    return true;
  }
};
/* code provided by PROGIEZ */

2042. Check if Numbers Are Ascending in a Sentence LeetCode Solution in Java

class Solution {
  public boolean areNumbersAscending(String s) {
    int prev = 0;

    for (final String token : s.split(" "))
      if (Character.isDigit(token.charAt(0))) {
        final int num = Integer.parseInt(token);
        if (num <= prev)
          return false;
        prev = num;
      }

    return true;
  }
}
// code provided by PROGIEZ

2042. Check if Numbers Are Ascending in a Sentence LeetCode Solution in Python

class Solution:
  def areNumbersAscending(self, s: str) -> bool:
    prev = 0

    for token in s.split():
      if token.isdigit():
        num = int(token)
        if num <= prev:
          return False
        prev = num

    return True
# code by PROGIEZ

Additional Resources

See also  1027. Longest Arithmetic Subsequence LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.