Welcome to Subscribe On Youtube
Formatted question description: https://leetcode.ca/all/2042.html
2042. Check if Numbers Are Ascending in a Sentence (Easy)
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.
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.
Example 4:
Input: s = "4 5 11 26" Output: true Explanation: The numbers in s are: 4, 5, 11, 26. They are strictly increasing from left to right: 4 < 5 < 11 < 26.
Constraints:
3 <= s.length <= 200
s
consists of lowercase English letters, spaces, and digits from0
to9
, inclusive.- The number of tokens in
s
is between2
and100
, 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 than100
, with no leading zeros. s
contains no leading or trailing spaces.
Similar Questions:
Solution 1. Brute Force
-
// OJ: https://leetcode.com/problems/check-if-numbers-are-ascending-in-a-sentence// // Time: O(N) // Space: O(N) class Solution { public: bool areNumbersAscending(string s) { string word; istringstream ss(s); int prev = -1; while (ss >> word) { if (isdigit(word[0])) { int n = stoi(word); if (n <= prev) return false; prev = n; } } return true; } };
-
class Solution: def areNumbersAscending(self, s: str) -> bool: pre = 0 for t in s.split(): if t[0].isdigit(): if (cur := int(t)) <= pre: return False pre = cur return True ############ # 2042. Check if Numbers Are Ascending in a Sentence # https://leetcode.com/problems/check-if-numbers-are-ascending-in-a-sentence/ class Solution: def areNumbersAscending(self, s: str) -> bool: s = s.split(' ') nums = [] for x in s: if x.isnumeric(): nums.append(int(x)) for i in range(1, len(nums)): if nums[i] <= nums[i - 1]: return False return True
-
class Solution { public boolean areNumbersAscending(String s) { int pre = 0; for (var t : s.split(" ")) { if (t.charAt(0) <= '9') { int cur = Integer.parseInt(t); if (pre >= cur) { return false; } pre = cur; } } return true; } }
-
func areNumbersAscending(s string) bool { pre := 0 for _, t := range strings.Split(s, " ") { if t[0] <= '9' { cur, _ := strconv.Atoi(t) if pre >= cur { return false } pre = cur } } return true }
-
function areNumbersAscending(s: string): boolean { let pre = -1; for (const cur of s.split(' ')) { if (cur[0] <= '9') { const num = Number(cur); if (num <= pre) { return false; } pre = num; } } return true; }
-
impl Solution { pub fn are_numbers_ascending(s: String) -> bool { let mut pre = -1; for cur in s.split(' ') { if cur.as_bytes()[0] <= b'9' { let num = cur.parse::<i32>().unwrap(); if num <= pre { return false; } pre = num; } } true } }
Discuss
https://leetcode.com/problems/check-if-numbers-are-ascending-in-a-sentence/discuss/1525203/C%2B%2B-Brute-Force