Skip to content

Instantly share code, notes, and snippets.

@KaitoMuraoka
Last active May 7, 2023 08:36
Show Gist options
  • Save KaitoMuraoka/90e7e87701a6105ea2e4f8f0ef9442e4 to your computer and use it in GitHub Desktop.
Save KaitoMuraoka/90e7e87701a6105ea2e4f8f0ef9442e4 to your computer and use it in GitHub Desktop.
関数を早めに抜ける
private func isSimilar(str1: String, str2: String) -> String {
var ok: String = "Yes"
// 条件1のチェック
if (str1.count != str2.count) {
ok = "No"
return ok
}
// 条件2のチェック
var diffCount = 0
for index in 0..<str1.count {
if str1[str1.index(str1.startIndex, offsetBy: index)] != str2[str2.index(str2.startIndex, offsetBy: index)] {
diffCount += 1
}
}
if diffCount != 1 {
ok = "No"
}
return ok
}
func main() {
let array = [
["aruru", "araru"],
["aruru", "arara"],
["aruru", "aruru"],
["algo", "all"],
["str", "std"]
]
for it in array {
print(isSimilar(str1: it[0], str2: it[1]))
}
}
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment