Skip to content

Commit 47355fb

Browse files
authored
feat: add swift implementation to lcci problem: No.16.20 (doocs#2763)
1 parent 9084f69 commit 47355fb

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

lcci/16.20.T9/README.md

+28
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,34 @@ function getValidT9Words(num: string, words: string[]): string[] {
154154
}
155155
```
156156

157+
```swift
158+
class Solution {
159+
func getValidT9Words(_ num: String, _ words: [String]) -> [String] {
160+
let s = "22233344455566677778889999"
161+
var d = Array(repeating: 0, count: 26)
162+
for i in 0..<26 {
163+
d[i] = Int(s[s.index(s.startIndex, offsetBy: i)].asciiValue! - Character("0").asciiValue!)
164+
}
165+
var ans: [String] = []
166+
let n = num.count
167+
for w in words {
168+
var ok = true
169+
for i in 0..<n {
170+
let numChar = Int(num[num.index(num.startIndex, offsetBy: i)].asciiValue! - Character("0").asciiValue!)
171+
if d[Int(w[w.index(w.startIndex, offsetBy: i)].asciiValue! - Character("a").asciiValue!)] != numChar {
172+
ok = false
173+
break
174+
}
175+
}
176+
if ok {
177+
ans.append(w)
178+
}
179+
}
180+
return ans
181+
}
182+
}
183+
```
184+
157185
<!-- tabs:end -->
158186

159187
<!-- end -->

lcci/16.20.T9/README_EN.md

+28
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,34 @@ function getValidT9Words(num: string, words: string[]): string[] {
160160
}
161161
```
162162

163+
```swift
164+
class Solution {
165+
func getValidT9Words(_ num: String, _ words: [String]) -> [String] {
166+
let s = "22233344455566677778889999"
167+
var d = Array(repeating: 0, count: 26)
168+
for i in 0..<26 {
169+
d[i] = Int(s[s.index(s.startIndex, offsetBy: i)].asciiValue! - Character("0").asciiValue!)
170+
}
171+
var ans: [String] = []
172+
let n = num.count
173+
for w in words {
174+
var ok = true
175+
for i in 0..<n {
176+
let numChar = Int(num[num.index(num.startIndex, offsetBy: i)].asciiValue! - Character("0").asciiValue!)
177+
if d[Int(w[w.index(w.startIndex, offsetBy: i)].asciiValue! - Character("a").asciiValue!)] != numChar {
178+
ok = false
179+
break
180+
}
181+
}
182+
if ok {
183+
ans.append(w)
184+
}
185+
}
186+
return ans
187+
}
188+
}
189+
```
190+
163191
<!-- tabs:end -->
164192

165193
<!-- end -->

lcci/16.20.T9/Solution.swift

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
func getValidT9Words(_ num: String, _ words: [String]) -> [String] {
3+
let s = "22233344455566677778889999"
4+
var d = Array(repeating: 0, count: 26)
5+
for i in 0..<26 {
6+
d[i] = Int(s[s.index(s.startIndex, offsetBy: i)].asciiValue! - Character("0").asciiValue!)
7+
}
8+
var ans: [String] = []
9+
let n = num.count
10+
for w in words {
11+
var ok = true
12+
for i in 0..<n {
13+
let numChar = Int(num[num.index(num.startIndex, offsetBy: i)].asciiValue! - Character("0").asciiValue!)
14+
if d[Int(w[w.index(w.startIndex, offsetBy: i)].asciiValue! - Character("a").asciiValue!)] != numChar {
15+
ok = false
16+
break
17+
}
18+
}
19+
if ok {
20+
ans.append(w)
21+
}
22+
}
23+
return ans
24+
}
25+
}

0 commit comments

Comments
 (0)