Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // MARK: - 205. Isomorphic Strings
- // s = "egg", t = "add"
- // строки изоморфны когда s изоморфна t
- // и t изоморфна s
- // Нужно 2 словаря и 2 проверки
- func isIsomorphic(_ s: String, _ t: String) -> Bool {
- let s = Array(s).map{String($0)}
- let t = Array(t).map{String($0)}
- var s_t = [String:String]()
- var t_s = [String:String]()
- for i in 0..<t.count {
- let sChar = s[i]
- let tChar = t[i]
- if s_t[sChar] != nil && s_t[sChar] != tChar {
- return false
- }
- s_t[sChar] = tChar
- // или
- if t_s[tChar] != nil && t_s[tChar] != sChar {
- return false
- }
- t_s[tChar] = sChar
- }
- return true
- }
- isIsomorphic("egg", "foo")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement