-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSolution.swift
65 lines (52 loc) · 1.64 KB
/
Solution.swift
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
//
// Solution.swift
// LeetCode
//
// Created by Nihad on 11/20/21.
//
import Foundation
class Solution {
func arrayStringsAreEqual(_ word1: [String], _ word2: [String]) -> Bool {
var (i, j) = (0, word2[0].startIndex) // word2 current array and char indices correspondingly
for word in word1 {
for char in word {
if i >= word2.count || char != word2[i][j] {
return false
}
if j == word2[i].index(before: word2[i].endIndex) {
i += 1
if i < word2.count {
j = word2[i].startIndex
}
} else {
j = word2[i].index(after: j)
}
}
}
return i >= word2.count
}
}
/*
// MARK: - Solution using normal indices like in literally any normal other programming language
class Solution {
func arrayStringsAreEqual(_ word1: [String], _ word2: [String]) -> Bool {
let word1: [[Character]] = word1.map { Array($0) }
let word2: [[Character]] = word2.map { Array($0) }
var (i, j) = (0, 0) // word2 current array and char indices correspondingly
for word in word1 {
for char in word {
if i >= word2.count || char != word2[i][j] {
return false
}
if j == word2[i].count - 1 {
i += 1
j = 0
} else {
j += 1
}
}
}
return i >= word2.count
}
}
*/