Strobogrammatic Number

🏠 ⬅️ ➡️

Given a string num which represents an integer, return true if num is a strobogrammatic number.

A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down).

Example 1:

Input: num = "69 " Output: true

Example 2:

Input: num = "88 " Output: true

Example 3:

Input: num = "962 " Output: false

Constraints:

  • 1 <= num.length <= 50
  • num consists of only digits.
  • num does not contain any leading zeros except for zero itself.

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

    character(len=50) :: num
    logical :: strobogrammatic

    ! Example 1
    num = "69"
    write(*,*) strobogrammatic(num), "Expected: True"

    ! Example 2
    num = "88"
    write(*,*) strobogrammatic(num), "Expected: True"

    ! Example 3
    num = "962"
    write(*,*) strobogrammatic(num), "Expected: False"

contains

    function strobogrammatic(num) result(res)
        implicit none
        character(len=*), intent(in) :: num
        logical :: res

        if (num == "") then
            res = .false.
            return
        end if

        if (num(1:1) /= num(len(num):len_trim(num))) then
            res = .false.
            return
        end if

        res = strobogrammatic(num(2:len(num)-1))

    end function strobogrammatic

end program main
Compiled
Executed
Correct
program strobogrammaticNumber
      implicit none

      integer :: i, j, k, n
      character(len=50) :: num
      logical :: isStrobogrammatic

      ! Read input
      read(*,*) num

      ! Check if num is a strobogrammatic number
      isStrobogrammatic = .true.
      do i = 1, n/2
         if (num(i:i) /= num(n-i+1:n-i)) then
            isStrobogrammatic = .false.
            exit
         end if
      end do

      ! Output result
      if (isStrobogrammatic) then
         write(*,*) "True"
      else
         write(*,*) "False"
      end if

      end program strobogrammaticNumber
🌐 Data from online sources
def isStrobogrammatic(num: str) -> bool:
    lookup = {'0': '0', '1': '1', '6': '9', '8': '8', '9': '6'}
    left, right = 0, len(num) - 1
    while left <= right:
        if num[left] not in lookup or lookup[num[left]] != num[right]:
            return False
        left += 1
        right -= 1
    return True

The algorithm uses a while loop and a dictionary (lookup) to check if the given string is strobogrammatic. The main idea is to iterate through the string with two pointers, left and right. The left pointer starts at the beginning of the string and right starts at the end.

For each step of the while loop: 1. Check if the character at the left pointer is in the lookup dictionary, if not, return false. 2. Check if the mapped (rotated) character of the character at the left pointer matches the character at the right pointer, if not, return false. 3. Increment left pointer and decrement right pointer.

If the loop finishes iterating through the string without returning false, then return true as the string is strobogrammatic.

🌐 Data from online sources
bool isStrobogrammatic(string num) {
    unordered_map<char, char> lookup {{'0', '0'}, {'1', '1'}, {'6', '9'}, {'8', '8'}, {'9', '6'}};
    int left = 0, right = num.size() - 1;
    while(left <= right) {
        if(lookup[num[left]] != num[right]) return false;
        left++;
        right--;
    }
    return true;
}

The algorithm uses a while loop and a dictionary (lookup) to check if the given string is strobogrammatic. The main idea is to iterate through the string with two pointers, left and right. The left pointer starts at the beginning of the string and right starts at the end.

For each step of the while loop: 1. Check if the character at the left pointer is in the lookup dictionary, if not, return false. 2. Check if the mapped (rotated) character of the character at the left pointer matches the character at the right pointer, if not, return false. 3. Increment left pointer and decrement right pointer.

If the loop finishes iterating through the string without returning false, then return true as the string is strobogrammatic.