-
Notifications
You must be signed in to change notification settings - Fork 0
/
LC_127.kt
50 lines (46 loc) · 1.5 KB
/
LC_127.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import java.util.*
class Solution {
fun ladderLength(beginWord: String, endWord: String, wordList: List<String>): Int {
val endwordIdx = wordList.indexOf(endWord)
if (endwordIdx==-1) {
return 0
}
var ans :Int = 0
val queue = LinkedList<Pair<String, Int>>();
queue.add(Pair(beginWord, 1))
val visit = BooleanArray(wordList.size)
while (!queue.isEmpty()) {
val cur = queue.pop()
queue.addAll(getNext(cur, visit, wordList))
if(visit[endwordIdx]) {
ans = cur.second+1
break
}
}
return ans
}
private fun getNext(cur: Pair<String, Int>, visit: BooleanArray, wordList: List<String>): List<Pair<String, Int>> {
val res = mutableListOf<Pair<String,Int>>()
for (i in wordList.indices){
if(!visit[i] and isOneDiff(cur.first, wordList[i])){
visit[i] = true
res.add(Pair(wordList[i], cur.second+1))
}
}
return res
}
private fun isOneDiff(str1: String, str2: String): Boolean {
var cnt: Int = 0
for (i in str1.indices) {
if (str1[i] != str2[i]) {
cnt += 1
}
if (cnt > 1) return false
}
return true
}
}
fun main() {
val arr = arrayOf("hot","dot","dog","lot","log","cog").toList()
Solution().ladderLength("hit","cog", arr)
}