ac solution code


  • 0

    Solution1. Two Pointers - time = O(n); space = O(1)

    Strobogrammatic numbers in 0-9 are:

    1. Single: 0 1 8
    2. Pair : 6/9

    The basic idea is:
    Check pairs of start/end char of num string: start + end

    func isStrobogrammatic(_ num: String) -> Bool {
        let pairs: Set<String> = ["00", "11", "88", "69", "96"] // Possible pairs of start/end
        let chs = Array(num)
        var start = 0, end = chs.count - 1
        while start <= end {                                    // Two pointers: check start + end
            if !pairs.contains( String(chs[start]) + String(chs[end]) ) {
                return false
            }
            start += 1                                          
            end -= 1
        }
        return true
    }
    

Log in to reply
 

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