Skip to content

feat: add swift implementation to lcci problem: No.17.22 #2817

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Swift Implementation for LCCI 17.22
  • Loading branch information
klever34 committed May 15, 2024
commit 68ad3751edd8155ff6380ddf8e2084440805a983
60 changes: 60 additions & 0 deletions lcci/17.22.Word Transformer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,66 @@ function findLadders(beginWord: string, endWord: string, wordList: string[]): st
}
```

```swift
class Solution {
private var ans = [String]()
private var wordList: [String]
private var endWord: String
private var vis: [Bool]

init(wordList: [String], endWord: String) {
self.wordList = wordList
self.endWord = endWord
self.vis = Array(repeating: false, count: wordList.count)
}

func findLadders(_ beginWord: String) -> [String] {
ans.append(beginWord)
if dfs(beginWord) {
return ans
} else {
return []
}
}

private func dfs(_ s: String) -> Bool {
if s == endWord {
return true
}
for i in 0..<wordList.count {
let t = wordList[i]
if vis[i] || !check(s, t) {
continue
}
vis[i] = true
ans.append(t)
if dfs(t) {
return true
}
ans.removeLast()
vis[i] = false
}
return false
}

private func check(_ s: String, _ t: String) -> Bool {
if s.count != t.count {
return false
}
var count = 0
for (sc, tc) in zip(s, t) {
if sc != tc {
count += 1
if count > 1 {
return false
}
}
}
return count == 1
}
}
```

<!-- tabs:end -->

<!-- end -->
60 changes: 60 additions & 0 deletions lcci/17.22.Word Transformer/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,66 @@ function findLadders(beginWord: string, endWord: string, wordList: string[]): st
}
```

```swift
class Solution {
private var ans = [String]()
private var wordList: [String]
private var endWord: String
private var vis: [Bool]

init(wordList: [String], endWord: String) {
self.wordList = wordList
self.endWord = endWord
self.vis = Array(repeating: false, count: wordList.count)
}

func findLadders(_ beginWord: String) -> [String] {
ans.append(beginWord)
if dfs(beginWord) {
return ans
} else {
return []
}
}

private func dfs(_ s: String) -> Bool {
if s == endWord {
return true
}
for i in 0..<wordList.count {
let t = wordList[i]
if vis[i] || !check(s, t) {
continue
}
vis[i] = true
ans.append(t)
if dfs(t) {
return true
}
ans.removeLast()
vis[i] = false
}
return false
}

private func check(_ s: String, _ t: String) -> Bool {
if s.count != t.count {
return false
}
var count = 0
for (sc, tc) in zip(s, t) {
if sc != tc {
count += 1
if count > 1 {
return false
}
}
}
return count == 1
}
}
```

<!-- tabs:end -->

<!-- end -->
57 changes: 57 additions & 0 deletions lcci/17.22.Word Transformer/Solution.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
class Solution {
private var ans = [String]()
private var wordList: [String]
private var endWord: String
private var vis: [Bool]

init(wordList: [String], endWord: String) {
self.wordList = wordList
self.endWord = endWord
self.vis = Array(repeating: false, count: wordList.count)
}

func findLadders(_ beginWord: String) -> [String] {
ans.append(beginWord)
if dfs(beginWord) {
return ans
} else {
return []
}
}

private func dfs(_ s: String) -> Bool {
if s == endWord {
return true
}
for i in 0..<wordList.count {
let t = wordList[i]
if vis[i] || !check(s, t) {
continue
}
vis[i] = true
ans.append(t)
if dfs(t) {
return true
}
ans.removeLast()
vis[i] = false
}
return false
}

private func check(_ s: String, _ t: String) -> Bool {
if s.count != t.count {
return false
}
var count = 0
for (sc, tc) in zip(s, t) {
if sc != tc {
count += 1
if count > 1 {
return false
}
}
}
return count == 1
}
}