Neat Java solution


  • 0
    C
    public class Solution {
        public boolean isStrobogrammatic(String num) {
            char[] ar = num.toCharArray();
            int i = 0;
            int j = ar.length -1;
            
            while(i<=j) { // must <= instead of <, as otherwise the single input '2' will fail
                if(!match(ar[i], ar[j])) {
                    return false;
                }
                i++; // must add ++ and --
                j--;
            }
            return true;
        }
        
        private boolean match(char a, char b) {
            if( ((a=='1') && (b=='1')) ||
                ((a=='6') && (b=='9')) ||
                ((a=='9') && (b=='6')) ||
                ((a=='8') && (b=='8')) ||
                ((a=='0') && (b=='0'))
              ) {
                  return true;
              }
              
            return false;
        }
    }
    
    

Log in to reply
 

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