2042. Check if Numbers Are Ascending in a Sentence Easy

@problem@discussion
#String



1/**
2 * [2042] Check if Numbers Are Ascending in a Sentence
3 *
4 * 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.
5 * 
6 * 	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.
7 * 
8 * 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).
9 * Return true if so, or false otherwise.
10 *  
11 * Example 1:
12 * <img alt="example-1" src="https://assets.leetcode.com/uploads/2021/09/30/example1.png" style="width: 637px; height: 48px;" />
13 * Input: s = "1 box has 3 blue 4 red 6 green and 12 yellow marbles"
14 * Output: true
15 * Explanation: The numbers in s are: 1, 3, 4, 6, 12.
16 * They are strictly increasing from left to right: 1 < 3 < 4 < 6 < 12.
17 * 
18 * Example 2:
19 * 
20 * Input: s = "hello world 5 x 5"
21 * Output: false
22 * Explanation: The numbers in s are: <u>5</u>, <u>5</u>. They are not strictly increasing.
23 * 
24 * Example 3:
25 * <img alt="example-3" src="https://assets.leetcode.com/uploads/2021/09/30/example3.png" style="width: 794px; height: 48px;" />
26 * Input: s = "sunset is at 7 51 pm overnight lows will be in the low 50 and 60 s"
27 * Output: false
28 * Explanation: The numbers in s are: 7, <u>51</u>, <u>50</u>, 60. They are not strictly increasing.
29 * 
30 *  
31 * Constraints:
32 * 
33 * 	3 <= s.length <= 200
34 * 	s consists of lowercase English letters, spaces, and digits from 0 to 9, inclusive.
35 * 	The number of tokens in s is between 2 and 100, inclusive.
36 * 	The tokens in s are separated by a single space.
37 * 	There are at least two numbers in s.
38 * 	Each number in s is a positive number less than 100, with no leading zeros.
39 * 	s contains no leading or trailing spaces.
40 * 
41 */
42pub struct Solution {}
43
44// problem: https://leetcode.com/problems/check-if-numbers-are-ascending-in-a-sentence/
45// discuss: https://leetcode.com/problems/check-if-numbers-are-ascending-in-a-sentence/discuss/?currentPage=1&orderBy=most_votes&query=
46
47// submission codes start here
48
49impl Solution {
50    pub fn are_numbers_ascending(s: String) -> bool {
51        false
52    }
53}
54
55// submission codes end
56
57#[cfg(test)]
58mod tests {
59    use super::*;
60
61    #[test]
62    fn test_2042() {
63    }
64}
65


Back
© 2025 bowen.ge All Rights Reserved.