3
3
using System . Linq ;
4
4
5
5
public class Solution {
6
- public int LadderLength ( string beginWord , string endWord , ISet < string > wordDict ) {
7
- if ( beginWord == endWord ) return 1 ;
8
- wordDict . Remove ( beginWord ) ;
9
- wordDict . Remove ( endWord ) ;
10
- var words = new [ ] { beginWord , endWord } . Concat ( wordDict . Where ( word => word . Length == beginWord . Length ) ) . Select ( ( word , i ) => new { Word = word , Index = i } ) . ToList ( ) ;
11
-
12
- var paths = new List < int > [ words . Count ] ;
13
- for ( var i = 0 ; i < paths . Length ; ++ i )
14
- {
15
- paths [ i ] = new List < int > ( ) ;
16
- }
17
- for ( var i = 0 ; i < beginWord . Length ; ++ i )
18
- {
6
+ public int LadderLength ( string beginWord , string endWord , IList < string > wordList ) {
7
+ var words = Enumerable . Repeat ( beginWord , 1 ) . Concat ( wordList ) . Select ( ( word , i ) => new { Word = word , Index = i } ) . ToList ( ) ;
8
+ var endWordIndex = words . Find ( w => w . Word == endWord ) ? . Index ;
9
+ if ( endWordIndex == null ) {
10
+ return 0 ;
11
+ }
12
+
13
+ var paths = new List < int > [ words . Count ] ;
14
+ for ( var i = 0 ; i < paths . Length ; ++ i )
15
+ {
16
+ paths [ i ] = new List < int > ( ) ;
17
+ }
18
+ for ( var i = 0 ; i < beginWord . Length ; ++ i )
19
+ {
19
20
var hashMap = new Hashtable ( ) ;
20
21
foreach ( var item in words )
21
22
{
22
23
var newWord = string . Format ( "{0}_{1}" , item . Word . Substring ( 0 , i ) , item . Word . Substring ( i + 1 ) ) ;
23
- List < int > similars ;
24
+ List < int > similars ;
24
25
if ( ! hashMap . ContainsKey ( newWord ) )
25
- {
26
- similars = new List < int > ( ) ;
27
- hashMap . Add ( newWord , similars ) ;
28
- }
29
- else
30
- {
31
- similars = ( List < int > ) hashMap [ newWord ] ;
32
- }
33
- foreach ( var similar in similars )
34
- {
35
- paths [ similar ] . Add ( item . Index ) ;
36
- paths [ item . Index ] . Add ( similar ) ;
37
- }
26
+ {
27
+ similars = new List < int > ( ) ;
28
+ hashMap . Add ( newWord , similars ) ;
29
+ }
30
+ else
31
+ {
32
+ similars = ( List < int > ) hashMap [ newWord ] ;
33
+ }
34
+ foreach ( var similar in similars )
35
+ {
36
+ paths [ similar ] . Add ( item . Index ) ;
37
+ paths [ item . Index ] . Add ( similar ) ;
38
+ }
38
39
similars . Add ( item . Index ) ;
39
40
}
40
- }
41
-
42
- var left = words . Count - 1 ;
43
- var lastRound = new List < int > { 0 } ;
44
- var visited = new bool [ words . Count ] ;
45
- visited [ 0 ] = true ;
46
- for ( var result = 2 ; left > 0 ; ++ result )
47
- {
48
- var thisRound = new List < int > ( ) ;
49
- foreach ( var index in lastRound )
50
- {
51
- foreach ( var next in paths [ index ] )
52
- {
53
- if ( ! visited [ next ] )
54
- {
55
- visited [ next ] = true ;
56
- if ( next == 1 ) return result ;
57
- thisRound . Add ( next ) ;
58
- }
59
- }
60
- }
41
+ }
42
+
43
+ var left = words . Count - 1 ;
44
+ var lastRound = new List < int > { 0 } ;
45
+ var visited = new bool [ words . Count ] ;
46
+ visited [ 0 ] = true ;
47
+ for ( var result = 2 ; left > 0 ; ++ result )
48
+ {
49
+ var thisRound = new List < int > ( ) ;
50
+ foreach ( var index in lastRound )
51
+ {
52
+ foreach ( var next in paths [ index ] )
53
+ {
54
+ if ( ! visited [ next ] )
55
+ {
56
+ visited [ next ] = true ;
57
+ if ( next == endWordIndex ) return result ;
58
+ thisRound . Add ( next ) ;
59
+ }
60
+ }
61
+ }
61
62
if ( thisRound . Count == 0 ) break ;
62
- lastRound = thisRound ;
63
- }
64
-
65
- return 0 ;
63
+ lastRound = thisRound ;
64
+ }
65
+
66
+ return 0 ;
66
67
}
67
68
}
0 commit comments