Check if Numbers Are Ascending in a Sentence

🏠 ⬅️ ➡️

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.

Constraints:

  • 3 <= s.length <= 200
  • s consists of lowercase English letters, spaces, and digits from 0 to 9, inclusive.
  • The number of tokens in s is between 2 and 100, 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 than 100, with no leading zeros.
  • s contains no leading or trailing spaces.

Note: This problem is from LeetCode.
Compiled
Executed
Correct
program main
    implicit none

    character(len=200) :: s
    logical :: result

    ! Examples taken from the problem statement
    s = "1 box has 3 blue 4 red 6 green and 12 yellow marbles "
    write (*, '(L1)') solve(s)
    s = "hello world 5 x 5 "
    write (*, '(L1)') solve(s)
    s = "sunset is at 7 51 pm overnight lows will be in the low 50 and 60 s "
    write (*, '(L1)') solve(s)

contains

    function solve(s) result(result)
        implicit none
        character(len=*), intent(in) :: s
        integer :: i, j, n
        logical :: result

        ! Split the input string into tokens
        call split(s, n)

        ! Check if all numbers are strictly increasing
        do i = 1, n - 1
            read (s(i+1:i+1), *) j
            if (j <= s(i:i)) then
                result = .false.
                return
            end if
        end do

        result = .true.

    end function solve

    subroutine split(string, n)
        implicit none
        character(len=*), intent(in) :: string
        integer, intent(out) :: n
        integer :: i, j

        n = 1
        do i = 1, len(string)
            if (string(i:i) == ' ') then
                n = n + 1
            end if
        end do

    end subroutine split

end program main
Compiled
Executed
Correct
program main
      implicit none

      integer :: i, j, n
      character(len=200) :: s
      character(len=200) :: tokens(100)
      logical :: is_strictly_increasing

      read(*,*) s

      n = 0
      do i = 1, len_trim(s)
          if (s(i:i) == ' ') then
              n = n + 1
              tokens(n) = s(i+1:len_trim(s))
          end if
      end do

      is_strictly_increasing = .true.
      do i = 1, n-1
          read(tokens(i),*) j
          read(tokens(i+1),*) k
          if (j >= k) then
              is_strictly_increasing = .false.
              exit
          end if
      end do

      if (is_strictly_increasing) then
          print *, "True"
      else
          print *, "False"
      end if

      end program main
🌐 Data from online sources
def maxProductDifference(nums):
    min1, min2, max1, max2 = float('inf'), float('inf'), float('-inf'), float('-inf')
    for num in nums:
        if num < min1:
            min2, min1 = min1, num
        elif num < min2:
            min2 = num
        if num > max1:
            max2, max1 = max1, num
        elif num > max2:
            max2 = num
    return max1 * max2 - min1 * min2

We need to find two smallest numbers (min1, min2) and two largest numbers (max1, max2) in the given array. Initialize all four variables with appropriate extreme values, then loop through the array and update them on each iteration. The maximum product difference is (max1 * max2) - (min1 * min2).

🌐 Data from online sources
int maxProductDifference(vector<int>& nums) {
    int min1 = INT_MAX, min2 = INT_MAX, max1 = INT_MIN, max2 = INT_MIN;
    for (int num : nums) {
        if (num < min1) {
            min2 = min1;
            min1 = num;
        } else if (num < min2) {
            min2 = num;
        }
        if (num > max1) {
            max2 = max1;
            max1 = num;
        } else if (num > max2) {
            max2 = num;
        }
    }
    return max1 * max2 - min1 * min2;
}

We need to find two smallest numbers (min1, min2) and two largest numbers (max1, max2) in the given array. Initialize all four variables with appropriate extreme values, then loop through the array and update them on each iteration. The maximum product difference is (max1 * max2) - (min1 * min2).