4 lines in Java

  • 155

    Just checking the pairs, going inwards from the ends.

    public boolean isStrobogrammatic(String num) {
        for (int i=0, j=num.length()-1; i <= j; i++, j--)
            if (!"00 11 88 696".contains(num.charAt(i) + "" + num.charAt(j)))
                return false;
        return true;

  • 0
    This post is deleted!

  • -2

    Well, this is smart solution.
    But how about the input string is "1=" or "8=" or "=1", etc, where "=" means space. It will return true but actually it is false. The test cases doesn't include above cases.
    I know this input might be invalid but the program is not robust enough..
    Just my 2 cents.

  • 6

    That's indeed invalid input. We're not asked to determine whether an arbitrary string represents a strobogrammatic number. We're asked to determine if a number is strobogrammatic. Which for technical reasons ends up as determining whether a string representing a number represents a strobogrammatic number.

    And if you think we should handle invalid input, then you should call your own three solutions "not robust enough" as well. I can make all of them crash.

  • 0

    Thanks for the comments. You're correct.

  • 0

    I thought I know for loops lol. Neat solution

  • 0

    This is really smart and clean, I like this!!

  • 0

    You can always think about the concise solution, smart!

  • 0

    Wow you are really smart

  • 0

    @StefanPochmann Hi, is the time complexity of this code O(N)?
    As the size of matcher string is always constant

  • 0

    Much smarter than using hashmap. Thanks for sharing!

  • 0

    Amazing solution! Thanks for sharing!

  • 3

    The pairs are actually 00 11 69 96 88

    696 looks smart, but a little bit confusing to read

  • 0

    Grrrrreat~ do not forget add validation before circle, which may cause NPE as num is null.

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.