Show problems as
Classic problem #246EasyHash TableTwo PointersString

Strobogrammatic Number

Real-world version: Does the Reading Survive a Half Turn
Tracked in this browser only
Read Strobogrammatic Number on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A reading is given as the string num, made only of digits.

Turning the whole reading upside down turns each digit in place and reverses their order. Only some digits survive the turn: 0, 1 and 8 turn into themselves, while 6 and 9 turn into each other. Every other digit becomes unreadable.

Return whether the reading looks exactly the same after being turned upside down.

Examples

Example 1

Input
num = "1961"
Output
true

The outer ones turn into each other, and the nine and six in the middle swap into each other, so the turned reading matches the original.

Example 2

Input
num = "25"
Output
false

The two has no turned form at all, so the reading cannot survive.

Example 3

Input
num = "0"
Output
true

A single zero turns into itself, so the reading is unchanged.

Constraints

  • 1 <= num.length <= 50
  • The reading is made of digits only.
  • The reading has no leading zero unless it is the single digit zero.

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def is_strobogrammatic(num: str) -> bool:
Java
public boolean isStrobogrammatic(String num)