Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4eeec1d

Browse files
Create Word Ladder
1 parent 69ec6e7 commit 4eeec1d

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed

‎精選高頻HARD題目/Word Ladder‎

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
想法:可把每個字串都想像成是一節點,而字串間如果只有一個字母有差異的話就會有邊相連
2+
那麼就可以用 BFS 找到最短路徑
3+
4+
// assume there is n strings in wordList , and each string length = m
5+
Time Complexity : O(mn^2) for O(n) nodes need O(nm) time to check if it exist in wordList and validity
6+
Space Complexity : O(n) for the queue
7+
8+
class Solution {
9+
public:
10+
bool valid( string &s1 , string& s2 ) {
11+
int count = 0 ;
12+
for(int i = 0 ; i < s1.length() ; i++)
13+
if ( s1[i] != s2[i] )
14+
count++ ;
15+
return count <= 1 ;
16+
}
17+
int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
18+
int target = -1 ;
19+
for(int i = 0 ; i < wordList.size() ; i++) {
20+
if ( endWord == wordList[i] ) {
21+
target = i ;
22+
break ;
23+
}
24+
}
25+
26+
queue<pair<int , int>> candidate ;
27+
wordList.push_back(beginWord) ;
28+
vector<int> finished(wordList.size() - 1) ;
29+
candidate.push( { wordList.size() - 1 , 1} ) ;
30+
31+
while ( !candidate.empty() ) {
32+
string now = wordList[candidate.front().first] ;
33+
int distance = candidate.front().second ;
34+
candidate.pop() ;
35+
for( int i = 0 ; i < wordList.size() - 1 ; i++ ) {
36+
if ( finished[i] == 1 )
37+
continue ;
38+
39+
if ( valid(wordList[i] , now) ) {
40+
if ( i == target )
41+
return distance + 1 ;
42+
else {
43+
candidate.push( {i , distance + 1} ) ;
44+
finished[i] = 1 ;
45+
}
46+
}
47+
}
48+
}
49+
return 0 ;
50+
}
51+
};
52+
53+
// another implementation
54+
55+
class Solution {
56+
public:
57+
bool valid(string &s1, string &s2) {
58+
int len = s1.length() ;
59+
int count = 0 ;
60+
for(int i = 0 ; i < len ; i++) {
61+
if ( s1[i] != s2[i] )
62+
count++ ;
63+
}
64+
return count == 1 ;
65+
}
66+
int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
67+
queue<int> candidate ;
68+
69+
int target = -1 ;
70+
vector<int> visited( wordList.size() ) ;
71+
for( int i = 0 ; i < wordList.size() ; i++) {
72+
if ( valid(beginWord , wordList[i]) ) {
73+
candidate.push(i) ;
74+
visited[i] = 1 ;
75+
}
76+
if ( endWord == wordList[i] )
77+
target = i ;
78+
}
79+
80+
if (target == -1)
81+
return 0 ;
82+
83+
int step = 2 ;
84+
while ( !candidate.empty() ) {
85+
int size = candidate.size() ;
86+
while ( size-- ) {
87+
int index = candidate.front() ;
88+
candidate.pop() ;
89+
if (index == target)
90+
return step ;
91+
for(int i = 0 ; i < wordList.size() ; i++) {
92+
if ( valid(wordList[index] , wordList[i]) && visited[i] != 1) {
93+
candidate.push(i) ;
94+
visited[i] = 1 ;
95+
}
96+
}
97+
}
98+
step++ ;
99+
}
100+
101+
return 0 ;
102+
}
103+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /