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.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
temp.f95:21:29: 21 | function strobogrammatic(num) result(res) | 1 Error: Procedure ‘strobogrammatic’ at (1) has an explicit interface from a previous declaration temp.f95:22:21: 22 | implicit none | 1 Error: Unexpected IMPLICIT NONE statement in CONTAINS section at (1) temp.f95:23:43: 23 | character(len=*), intent(in) :: num | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:24:22: 24 | logical :: res | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:26:27: 26 | if (num == "") then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:27:25: 27 | res = .false. | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:28:18: 28 | return | 1 Error: Unexpected RETURN statement in CONTAINS section at (1) temp.f95:29:11: 29 | end if | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:31:57: 31 | if (num(1:1) /= num(len(num):len_trim(num))) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:32:25: 32 | res = .false. | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:33:18: 33 | return | 1 Error: Unexpected RETURN statement in CONTAINS section at (1) temp.f95:34:11: 34 | end if | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:36:48: 36 | res = strobogrammatic(num(2:len(num)-1)) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:38:7: 38 | end function strobogrammatic | 1 Error: Expecting END PROGRAM statement at (1)
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
At line 9 of file temp.f95 (unit = 5, file = 'stdin') Fortran runtime error: End of file Error termination. Backtrace: #0 0x78df65d40960 in ??? #1 0x78df65d414d9 in ??? #2 0x78df65f9517b in ??? #3 0x78df65f8e684 in ??? #4 0x78df65f8f2aa in ??? #5 0x56dfb7ffd21a in MAIN__ #6 0x56dfb7ffd3ba in main
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.
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.