Swift. Use a dictionary.


  • 0
    M

    Used the same solution as check word pattern. I haven't figured out what's the difference between checking isomorphoic and word pattern.

    func isIsomorphic(_ s: String, _ t: String) -> Bool {
        return encode(s) == encode(t)
    }
    
    private func encode(_ s: String) -> String {
            var result = ""
            var letterMap = [Character : Int]()
            var start = 0
            for c in s.characters {
                if letterMap[c] == nil {
                    letterMap[c] = start
                    start += 1
                }
                result = result + "\(letterMap[c])"
            }
            return result
        }

Log in to reply
 

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