Check if Numbers Are Ascending in a Sentence — Solution
Check if Numbers Are Ascending in a Sentence solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript.
Check if Numbers Are Ascending in a Sentence is an easy coding problem on strings and simulation, asked in Amazon and Microsoft interviews.
- Difficulty: Easy
- Topics: Strings, Simulation
- Asked at: Amazon, Microsoft
- Time limit: 2 s
- Memory limit: 256 MB
- Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby
Problem statement
A sentence s is a list of tokens separated by single spaces. Every token is either a positive number written without leading zeros, or a word made of lowercase letters.
Return true if the numbers in s, read from left to right, are strictly increasing — each number is smaller than the next one — and false otherwise.
Example 1
Input: s = "kairo solved 3 bugs in 7 minutes and 12 duels"
Output: true
Explanation: 3 < 7 < 12.
Example 2
Input: s = "level 5 then 5 again"
Output: false
Explanation: 5 is not smaller than 5.
Example 3
Input: s = "rank 40 dropped to 9 today"
Output: false
Constraints
3 <= s.length <= 200s consists of lowercase English letters, spaces and digits from 0 to 9the number of tokens in s is between 2 and 100the tokens are separated by a single space, with no leading or trailing spacesthere are at least two numbers in seach number in s is a positive number less than 100, with no leading zeros
Hints for Check if Numbers Are Ascending in a Sentence
Hint 1
Split the sentence into tokens and keep only the numeric ones.
Hint 2
A token is a number exactly when its first character is a digit.
Hint 3
Remember the previous number (start at 0, since numbers are positive) and fail as soon as a number is not larger.
How to solve Check if Numbers Are Ascending in a Sentence
Only the numeric tokens matter, and "strictly increasing" is a property of each adjacent pair — so scan once, remembering the previous number.
Approach
- Set
prev = 0. - For each token that starts with a digit, parse it as
v. - If
v <= prev, returnfalse; otherwise setprev = v. - Return
trueafter the scan.
Why it works
A sequence is strictly increasing exactly when every number is larger than the one before it, which is the check made at each step. Starting prev at 0 is safe because every number is positive.
Complexity
- Time —
O(n) - Space —
O(1)
Pitfalls
- Equal neighbours break strictness —
5 5is not ascending. - Compare numerically, not as strings:
"9" > "12"as text. - Numbers can have two digits; parse the whole token.
Reference solution
Python
def areNumbersAscending(s: str) -> bool:
prev = 0
for tok in s.split():
if tok[0].isdigit():
v = int(tok)
if v <= prev:
return False
prev = v
return TrueJava
public static boolean areNumbersAscending(String s) {
int prev = 0;
for (String tok : s.split(" ")) {
char c = tok.charAt(0);
if (c >= '0' && c <= '9') {
int v = Integer.parseInt(tok);
if (v <= prev) return false;
prev = v;
}
}
return true;
}C++
bool areNumbersAscending(string s) {
istringstream in1(s);
string tok;
int prev = 0;
while (in1 >> tok) {
if (tok[0] >= '0' && tok[0] <= '9') {
int v = stoi(tok);
if (v <= prev) return false;
prev = v;
}
}
return true;
}JavaScript
var areNumbersAscending = function(s) {
var toks = s.split(" ");
var prev = 0;
for (var i = 0; i < toks.length; i++) {
var c = toks[i][0];
if (c >= "0" && c <= "9") {
var v = parseInt(toks[i], 10);
if (v <= prev) return false;
prev = v;
}
}
return true;
};Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.
All 515 strings problems · the whole catalogue
Learn the technique: Strings