-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchangeWord.go
65 lines (53 loc) · 1.23 KB
/
changeWord.go
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package programmers
import (
"strconv"
)
func SolutionChangeWord(begin string, target string, words []string) int {
// Check if the target word is in the list of words
found := false
for _, word := range words {
if word == target {
found = true
break
}
}
if !found {
return 0
}
// Initialize the visited array
checkArr := make([]bool, len(words))
return bfs(begin, target, words, checkArr)
}
func bfs(begin string, target string, words []string, checkArr []bool) int {
queue := [][]string{}
queue = append(queue, []string{begin, "0"})
wordLength := len(begin)
for len(queue) > 0 {
current := queue[0]
queue = queue[1:]
currentWord := current[0]
currentSteps, err := strconv.Atoi(current[1])
if err != nil {
continue // 숫자로 변환할 수 없는 경우 생략
}
if currentWord == target {
return currentSteps
}
for idx, anotherWord := range words {
if !checkArr[idx] {
count := 0
for i := 0; i < wordLength; i++ {
if currentWord[i] == anotherWord[i] {
count++
}
}
if count == wordLength-1 {
checkArr[idx] = true
nextSteps := currentSteps + 1
queue = append(queue, []string{anotherWord, strconv.Itoa(nextSteps)})
}
}
}
}
return 0
}