Skip to main content

Command Palette

Search for a command to run...

Check if Numbers Are Ascending in a Sentence

Published
2 min read
Check if Numbers Are Ascending in a Sentence
G

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.

I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.

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.

LeetCode Problem - 2042

class Solution {
    public boolean areNumbersAscending(String s) {
        // Split the input string into an array of words/numbers
        String[] numbers = s.split(" ");

        int prev = 0; // Variable to track the previous number
        int curr = 0; // Variable to track the current number
        int count = 0; // Counter to check if we've found any valid number

        // Loop through each word in the numbers array
        for(int i = 0; i < numbers.length; i++) {
            try {
                // Try to parse the current word into an integer
                curr = Integer.parseInt(numbers[i]);

                // If this is the first valid number found
                if(count == 0) {
                    prev = curr; // Set prev to curr
                    count++; // Increment count to indicate we found a number
                } 
                // If the current number is greater than the previous number
                else if(curr > prev) {
                    prev = curr; // Update prev to the current number
                } 
                // If the current number is not greater than the previous number, return false
                else return false;

            } catch (Exception e) {
                // If an exception occurs (e.g., if the word is not a number), continue to the next word
                continue;
            }
        }
        // If all numbers are in ascending order, return true
        return true;
    }
}

More from this blog

S

Software and Performance Testing Insights

462 posts

Results-Driven Agile QA Specialist | Expert in Mobile & Web Testing | Proficient in Test Planning, Execution, and Root Cause Analysis.