Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Find and Replace
class Solution {
func findAndReplacePattern(_ words: [String], _ pattern: String) -> [String] {
var output = [String]()
for word in words {
if isMatch(word,pattern) {
output.append(word)
}
}
return output
}
func isMatch(_ word:String, _ pattern:String) -> Bool {
var mapW = [Character:Character]()
var mapP = [Character:Character]()
for (w,p) in zip(word,pattern){
if mapW[w] == nil{
mapW[w] = p
}
if mapP[p] == nil{
mapP[p] = w
}
if mapW[w] != p || mapP[p] != w {
return false
}
}
return true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment