|
| 1 | +--- |
| 2 | +id: check-if-numbers-are-ascending-in-a-sentence |
| 3 | +title: Check If Numbers are Ascending in a Sentence |
| 4 | +sidebar_label: 2042-Check If Numbers are Ascending in a Sentence |
| 5 | +tags: |
| 6 | + - Valid Numbers |
| 7 | + - Brute Force |
| 8 | + - Optimized |
| 9 | + - LeetCode |
| 10 | + - Java |
| 11 | + - Python |
| 12 | + - C++ |
| 13 | +description: "This is a solution to the Check If Numbers are Ascending in a Sentence on LeetCode." |
| 14 | +sidebar_position: 2 |
| 15 | +--- |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | +## Problem Description |
| 20 | + |
| 21 | +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. |
| 22 | + |
| 23 | +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. |
| 24 | +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). |
| 25 | + |
| 26 | +Return true if so, or false otherwise. |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | +### Examples |
| 31 | + |
| 32 | +**Example 1:** |
| 33 | + |
| 34 | +``` |
| 35 | +Input: s = "1 box has 3 blue 4 red 6 green and 12 yellow marbles" |
| 36 | +Output: true |
| 37 | +Explanation: The numbers in s are: 1, 3, 4, 6, 12. |
| 38 | +They are strictly increasing from left to right: 1 < 3 < 4 < 6 < 12. |
| 39 | +``` |
| 40 | + |
| 41 | +**Example 2:** |
| 42 | + |
| 43 | +``` |
| 44 | +Input: s = "hello world 5 x 5" |
| 45 | +Output: false |
| 46 | +Explanation: The numbers in s are: 5, 5. They are not strictly increasing. |
| 47 | +``` |
| 48 | + |
| 49 | +**Example 3:** |
| 50 | + |
| 51 | +``` |
| 52 | +Input: s = "sunset is at 7 51 pm overnight lows will be in the low 50 and 60 s" |
| 53 | +Output: false |
| 54 | +Explanation: The numbers in s are: 7, 51, 50, 60. They are not strictly increasing. |
| 55 | +``` |
| 56 | + |
| 57 | +### Constraints |
| 58 | + |
| 59 | +- `3 <= s.length <= 200` |
| 60 | +- `s` consists of lowercase English letters, spaces, and digits from 0 to 9, inclusive. |
| 61 | +- The number of tokens in `s` is between 2 and 100, inclusive. |
| 62 | +- The tokens in `s` are separated by a single space. |
| 63 | +- There are at least two numbers in `s`. |
| 64 | +- Each number in `s` is a positive number less than 100, with no leading zeros. |
| 65 | +- `s` contains no leading or trailing spaces. |
| 66 | + |
| 67 | +--- |
| 68 | + |
| 69 | +## Solution for Strictly Increasing Numbers in Sentence Problem |
| 70 | + |
| 71 | +### Intuition and Approach |
| 72 | + |
| 73 | +The problem can be solved using a brute force approach or an optimized approach. The brute force approach directly iterates through the tokens of the sentence, extracts the numbers, and checks if they are strictly increasing. The optimized approach ensures efficient token processing and number validation. |
| 74 | + |
| 75 | +<Tabs> |
| 76 | +<tabItem value="Brute Force" label="Brute Force"> |
| 77 | + |
| 78 | +### Approach 1: Brute Force |
| 79 | + |
| 80 | +The brute force approach iterates through each token of the sentence, extracts the numbers, and checks if they are strictly increasing. |
| 81 | + |
| 82 | +#### Code in Different Languages |
| 83 | + |
| 84 | +<Tabs> |
| 85 | +<TabItem value="C++" label="C++" default> |
| 86 | +<SolutionAuthor name="@ImmidiSivani"/> |
| 87 | + |
| 88 | +```cpp |
| 89 | +#include <string> |
| 90 | +#include <sstream> |
| 91 | + |
| 92 | +class Solution { |
| 93 | +public: |
| 94 | + bool areNumbersAscending(std::string s) { |
| 95 | + std::istringstream iss(s); |
| 96 | + std::string token; |
| 97 | + int prevNumber = -1; |
| 98 | + |
| 99 | + while (iss >> token) { |
| 100 | + if (isdigit(token[0])) { |
| 101 | + int number = std::stoi(token); |
| 102 | + if (number <= prevNumber) { |
| 103 | + return false; |
| 104 | + } |
| 105 | + prevNumber = number; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + return true; |
| 110 | + } |
| 111 | +}; |
| 112 | +``` |
| 113 | +
|
| 114 | +</TabItem> |
| 115 | +<TabItem value="Java" label="Java"> |
| 116 | +<SolutionAuthor name="@ImmidiSivani"/> |
| 117 | +
|
| 118 | +```java |
| 119 | +class Solution { |
| 120 | + public boolean areNumbersAscending(String s) { |
| 121 | + String[] tokens = s.split(" "); |
| 122 | + int prevNumber = -1; |
| 123 | + |
| 124 | + for (String token : tokens) { |
| 125 | + if (Character.isDigit(token.charAt(0))) { |
| 126 | + int number = Integer.parseInt(token); |
| 127 | + if (number <= prevNumber) { |
| 128 | + return false; |
| 129 | + } |
| 130 | + prevNumber = number; |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + return true; |
| 135 | + } |
| 136 | +} |
| 137 | +``` |
| 138 | + |
| 139 | +</TabItem> |
| 140 | +<TabItem value="Python" label="Python"> |
| 141 | +<SolutionAuthor name="@ImmidiSivani"/> |
| 142 | + |
| 143 | +```python |
| 144 | +class Solution: |
| 145 | + def are_numbers_ascending(self, s: str) -> bool: |
| 146 | + tokens = s.split() |
| 147 | + prev_number = -1 |
| 148 | + |
| 149 | + for token in tokens: |
| 150 | + if token.isdigit(): |
| 151 | + number = int(token) |
| 152 | + if number <= prev_number: |
| 153 | + return False |
| 154 | + prev_number = number |
| 155 | + |
| 156 | + return True |
| 157 | +``` |
| 158 | + |
| 159 | +</TabItem> |
| 160 | +</Tabs> |
| 161 | + |
| 162 | +#### Complexity Analysis |
| 163 | + |
| 164 | +- Time Complexity: $O(n)$ |
| 165 | +- Space Complexity: $O(n)$ |
| 166 | +- Where `n` is the length of the sentence. |
| 167 | +- The time complexity is $O(n)$ because we iterate through the tokens once. |
| 168 | +- The space complexity is $O(n)$ because we store the tokens in a list. |
| 169 | + |
| 170 | +</tabItem> |
| 171 | +<tabItem value="Optimized" label="Optimized"> |
| 172 | + |
| 173 | +### Approach 2: Optimized Approach |
| 174 | + |
| 175 | +The optimized approach uses similar logic but includes improvements such as pre-checking token conditions or using efficient string methods. |
| 176 | + |
| 177 | +#### Code in Different Languages |
| 178 | + |
| 179 | +<Tabs> |
| 180 | +<TabItem value="C++" label="C++" default> |
| 181 | +<SolutionAuthor name="@ImmidiSivani"/> |
| 182 | + |
| 183 | +```cpp |
| 184 | +#include <string> |
| 185 | +#include <sstream> |
| 186 | + |
| 187 | +class Solution { |
| 188 | +public: |
| 189 | + bool areNumbersAscending(std::string s) { |
| 190 | + std::istringstream iss(s); |
| 191 | + std::string token; |
| 192 | + int prevNumber = -1; |
| 193 | + |
| 194 | + while (iss >> token) { |
| 195 | + if (std::all_of(token.begin(), token.end(), ::isdigit)) { |
| 196 | + int number = std::stoi(token); |
| 197 | + if (number <= prevNumber) { |
| 198 | + return false; |
| 199 | + } |
| 200 | + prevNumber = number; |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + return true; |
| 205 | + } |
| 206 | +}; |
| 207 | +``` |
| 208 | +
|
| 209 | +</TabItem> |
| 210 | +<TabItem value="Java" label="Java"> |
| 211 | +<SolutionAuthor name="@ImmidiSivani"/> |
| 212 | +
|
| 213 | +```java |
| 214 | +import java.util.*; |
| 215 | +
|
| 216 | +class Solution { |
| 217 | + public boolean areNumbersAscending(String s) { |
| 218 | + String[] tokens = s.split(" "); |
| 219 | + int prevNumber = -1; |
| 220 | + |
| 221 | + for (String token : tokens) { |
| 222 | + if (token.chars().allMatch(Character::isDigit)) { |
| 223 | + int number = Integer.parseInt(token); |
| 224 | + if (number <= prevNumber) { |
| 225 | + return false; |
| 226 | + } |
| 227 | + prevNumber = number; |
| 228 | + } |
| 229 | + } |
| 230 | + |
| 231 | + return true; |
| 232 | + } |
| 233 | +} |
| 234 | +``` |
| 235 | + |
| 236 | +</TabItem> |
| 237 | +<TabItem value="Python" label="Python"> |
| 238 | +<SolutionAuthor name="@ImmidiSivani"/> |
| 239 | + |
| 240 | +```python |
| 241 | +class Solution: |
| 242 | + def are_numbers_ascending(self, s: str) -> bool: |
| 243 | + tokens = s.split() |
| 244 | + prev_number = -1 |
| 245 | + |
| 246 | + for token in tokens: |
| 247 | + if token.isdigit(): |
| 248 | + number = int(token) |
| 249 | + if number <= prev_number: |
| 250 | + return False |
| 251 | + prev_number = number |
| 252 | + |
| 253 | + return True |
| 254 | +``` |
| 255 | + |
| 256 | +</TabItem> |
| 257 | +</Tabs> |
| 258 | + |
| 259 | +#### Complexity Analysis |
| 260 | + |
| 261 | +- Time Complexity: $O(n)$ |
| 262 | +- Space Complexity: $O(n)$ |
| 263 | +- Where `n` is the length of the sentence. |
| 264 | +- The time complexity is $O(n)$ because we iterate through the tokens once. |
| 265 | +- The space complexity is $O(n)$ because we store the tokens in a list. |
| 266 | + |
| 267 | +</tabItem> |
| 268 | +</Tabs> |
| 269 | + |
| 270 | +--- |
| 271 | + |
| 272 | +<h2>Authors:</h2> |
| 273 | + |
| 274 | +<div style={{display: 'flex', flexWrap: 'wrap', justifyContent: 'space-between', gap: '10px'}}> |
| 275 | +{['ImmidiSivani'].map(username => ( |
| 276 | + <Author key={username} username={username} /> |
| 277 | +))} |
| 278 | +</div> |
0 commit comments