File tree Expand file tree Collapse file tree 1 file changed +8
-4
lines changed Expand file tree Collapse file tree 1 file changed +8
-4
lines changed Original file line number Diff line number Diff line change @@ -38,7 +38,7 @@ class AlienDictionary {
38
38
}
39
39
40
40
private func initGraph( _ words: [ String ] ) -> ( [ Character : Int ] , [ Character : [ Character ] ] ) {
41
- var indegrees = [ Character: Int] ( ) , charToChars = [ Character: [ Character] ] ( )
41
+ var indegrees = [ Character: Int] ( ) , charToChars = [ Character: [ Character]
42
42
43
43
// init indegress and charToChars
44
44
words. forEach { word in
@@ -51,15 +51,19 @@ class AlienDictionary {
51
51
// refactor indegress and charToChars based on words
52
52
for i in 0 ..< words. count - 1 {
53
53
let currentWord = Array ( words [ i] ) , nextWord = Array ( words [ i + 1 ] )
54
- var j = 0
55
54
56
- while j < currentWord. count && j < nextWord. count {
55
+ for j in 0 ..< min ( currentWord. count, nextWord. count) {
57
56
let currentChar = currentWord [ j] , nextChar = nextWord [ j]
58
57
59
58
if nextChar == currentChar {
60
- j += 1
59
+
60
+ if j + 1 == right. count && right. count < left. count {
61
+ return ( [ Character: Int] ( ) , [ Character: [ Character] ] ( ) )
62
+ }
63
+
61
64
continue
62
65
}
66
+
63
67
if let toChars = charToChars [ currentChar] , toChars. contains ( nextChar) {
64
68
break
65
69
}
You can’t perform that action at this time.
0 commit comments