Strobogrammatic Number
Check whether a number still reads the same after you turn it upside down.
Some numbers read the same after you turn them upside down, that is, rotate them by 180 degrees. Such a number is called strobogrammatic. Given a number as a string numtruefalse
Example 1
Output: true
Example 2
Output: true
Example 3
Output: false
The Idea
Write 69 on a sticky note and turn the note upside down. The 6 becomes a 9 and the 9 becomes a 6, and they also swap places. The 9 that sat on the right is now on the left, as a 6, so the note still reads 69. A note with 88 reads 88 again. With 962, the 2 at the end turns into a shape that is no digit at all.
So turning a number does two things at once: every digit changes into its upside-down form, and the order of the digits is reversed. Which digits have an upside-down form? The top of the picture, labelled "each digit turned upside down", shows them.
The blue boxes show that 0, 1 and 8 stay the same, 6 becomes 9 and 9 becomes 6. The five boxes on the right, with the grey digits 2, 3, 4, 5 and 7, are marked "no digit upside down". A number that contains any of them can never be strobogrammatic.
Now the reversed order. After the turn, the first place shows the last digit, upside down, and that must equal the first digit we started with. The second place shows the second-to-last digit, upside down, and so on. Do we have to build the turned number to check this?
No. Because 6 and 9 swap and the other three stay put, we can just as well turn the left digit and compare it with the right one. Look at the row labelled "69": the left digit (lo) is 6, upside down it is 9, and the right digit (hi) is 9, a match. In the row labelled "962", the left digit 9 turns into 6, but the right digit is 2, so 962 is not strobogrammatic.
So a number is strobogrammatic when its digits pair up from the outside in: the first with the last, the second with the second-to-last, and in each pair the left digit turned upside down is the right digit.
Solution Approach
Four decisions remain: how to look up a digit's upside-down form, how to walk the pairs, what to do with a middle digit that has no partner, and whether to treat the input as a number at all.
Looking up a digit upside down
Where does the code keep the blue boxes from the top of the picture? Two short strings, lined up position by position, are enough: "01689""01986"
Finding a digit's position with indexOf"01689"indexOf
Walking the pairs from both ends
How do we visit the pairs? Keep two positions, lohilohifalse
Is it enough to check that every digit is one of 0, 1, 6, 8 and 9? No. Upside down, 66 reads 99 and 16 reads 91, so each digit has to be compared with the digit opposite it, not only checked on its own.
A simpler way also works: walk the string from the last digit to the first, write down each digit's upside-down form, and compare the new string with numnum
The middle digit
What happens in "818", which has an odd number of digits? The outer 8s pair up, and then lohi
The loop runs while lo <= hi
Keeping the input a string
Should the code turn numlong% 10longcharAt
Java Solution
The method isStrobogrammatic
- Before the method, two constants hold the lookup:
, the digits that turn, andTURNS = "01689" , what each becomes, at the same positions.BECOMES = "01986" - Start
at 0 andlo athi , the two ends of the string.num.length() - 1 - While
, find the digit atlo <= hi inlo withTURNS and keep its position inindexOf . Ifk is -1, the digit has no upside-down form, so returnk .false - Compare
, the left digit upside down, with the digit atBECOMES.charAt(k) . If they differ, returnhi . Otherwise movefalse right andlo left.hi - If the loop ends without a failed pair, every pair matched, so return
.true
The main
public class StrobogrammaticNumber {
private static final String TURNS = "01689"; // digits that stay digits upside down
private static final String BECOMES = "01986"; // what each one turns into
public static boolean isStrobogrammatic(String num) {
int lo = 0;
int hi = num.length() - 1;
while (lo <= hi) {
int k = TURNS.indexOf(num.charAt(lo));
if (k < 0) {
return false; // not 0, 1, 6, 8 or 9: no digit upside down
}
if (BECOMES.charAt(k) != num.charAt(hi)) {
return false; // the digit opposite is not its partner
}
lo++;
hi--;
}
return true;
}
public static void main(String[] args) {
String longNum = "6" + "0".repeat(18) + "9"; // 20 digits, too long for a long
String[] tests = {"69", "88", "962", "818", "8", "6", "16", "66", "609", "0", "-69",
longNum};
for (String num : tests) {
System.out.println(num + " -> " + isStrobogrammatic(num));
}
}
}
Running it prints:
69 -> true
88 -> true
962 -> false
818 -> true
8 -> true
6 -> false
16 -> false
66 -> false
609 -> true
0 -> true
-69 -> false
60000000000000000009 -> true
The three examples come out as expected: 69 and 88 are truefalse818 -> true609 -> truefalse
That middle check depends on lo <= hilo < hitrue
Look at 16 -> false66 -> falseTURNSLong.parseLongNumberFormatExceptiontrue
Complexity
The method checks each pair of opposite digits at most once and keeps only a few positions.
- Time: O(n), because
andlo meet after about n / 2 steps along the n digits ofhi , and each step is onenum in the five-characterindexOf .TURNS - Space: O(1), because the method keeps only
,lo andhi , and the two lookup strings stay five characters long whatever the length ofk .num
Checking a number from both ends toward the middle also decides