Skip to content

Commit e0bc990

Browse files
committed
Add solution to Strobogrammatic Number I and II
1 parent 22c9c67 commit e0bc990

File tree

3 files changed

+92
-3
lines changed

3 files changed

+92
-3
lines changed

Array/StrobogrammaticNumber.swift

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/strobogrammatic-number/
3+
* Primary idea: Two pointers, compare two characters until they are all valid
4+
*
5+
* Time Complexity: O(n), Space Complexity: O(1)
6+
*
7+
*/
8+
9+
class StrobogrammaticNumber {
10+
func isStrobogrammatic(_ num: String) -> Bool {
11+
let numChars = Array(num)
12+
var i = 0, j = num.count - 1
13+
14+
while i <= j {
15+
if isValid(numChars[i], numChars[j]) {
16+
i += 1
17+
j -= 1
18+
} else {
19+
return false
20+
}
21+
}
22+
23+
return true
24+
}
25+
26+
fileprivate func isValid(_ charA: Character, _ charB: Character) -> Bool {
27+
if charA == charB {
28+
return ["0", "1", "8"].contains(charA)
29+
} else {
30+
if (charA == "6" && charB == "9") || (charA == "9" && charB == "6") {
31+
return true
32+
} else {
33+
return false
34+
}
35+
}
36+
}
37+
}
38+

DFS/StrobogrammaticNumberII.swift

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/strobogrammatic-number-ii/
3+
* Primary idea: Classic DFS, set two places with correspond characters;
4+
* starting from head and tail, and then move towards middle to cover all places.
5+
6+
* Time Complexity: O(m^n), here m is 5; Space Complexity: O(n)
7+
*
8+
*/
9+
10+
class StrobogrammaticNumberII {
11+
func findStrobogrammatic(_ n: Int) -> [String] {
12+
var res = [String]()
13+
14+
guard n >= 1 else {
15+
return res
16+
}
17+
18+
let left = Array("01689"), right = Array("01986")
19+
var path = Array(repeating: Character("-"), count: n)
20+
21+
dfs(&res, left, right, 0, n - 1, &path)
22+
23+
return res
24+
}
25+
26+
fileprivate func dfs(_ res: inout [String], _ left: [Character], _ right: [Character], _ leftIdx: Int, _ path: inout [Character]) {
27+
let rightIdx = path.count - leftIdx - 1
28+
29+
if leftIdx > rightIdx {
30+
res.append(String(path))
31+
return
32+
}
33+
34+
for i in 0..<left.count {
35+
if leftIdx == 0 && leftIdx != rightIdx && left[i] == "0" {
36+
continue
37+
}
38+
39+
if leftIdx == rightIdx && (left[i] == "6" || left[i] == "9") {
40+
continue
41+
}
42+
43+
path[leftIdx] = left[i]
44+
path[rightIdx] = right[i]
45+
46+
dfs(&res, left, right, leftIdx + 1, rightIdx - 1, &path)
47+
}
48+
}
49+
}

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
* [Microsoft](#microsoft)
2929

3030
## Progress
31-
[Problem Status](#problem-status) shows the latest progress to all 800+ questions. Currently we have 253 completed solutions. Note: questions with &hearts; mark means that you have to **Subscript to premium membership** of LeetCode to unlock them. Thank you for great contributions from [CharleneJiang](https://github.com/CharleneJiang), [ReadmeCritic](https://github.com/ReadmeCritic), [demonkoo](https://github.com/demonkoo), [DaiYue](https://github.com/DaiYue), [Quaggie](https://github.com/Quaggie) and [jindulys](https://github.com/jindulys).
31+
[Problem Status](#problem-status) shows the latest progress to all 800+ questions. Currently we have 255 completed solutions. Note: questions with &hearts; mark means that you have to **Subscript to premium membership** of LeetCode to unlock them. Thank you for great contributions from [CharleneJiang](https://github.com/CharleneJiang), [ReadmeCritic](https://github.com/ReadmeCritic), [demonkoo](https://github.com/demonkoo), [DaiYue](https://github.com/DaiYue), [Quaggie](https://github.com/Quaggie) and [jindulys](https://github.com/jindulys).
3232

3333

3434
## Array
@@ -48,6 +48,7 @@
4848
[Remove Duplicates from Sorted Array II](https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/)| [Swift](./Array/RemoveDuplicatesFromSortedArrayII.swift)| Medium| O(n)| O(1)|
4949
[Move Zeroes](https://leetcode.com/problems/move-zeroes/)| [Swift](./Array/MoveZeroes.swift)| Easy| O(n)| O(1)|
5050
[Remove Element](https://leetcode.com/problems/remove-element/)| [Swift](./Array/RemoveElement.swift)| Easy| O(n)| O(1)|
51+
[Strobogrammatic Number](https://leetcode.com/problems/strobogrammatic-number/)| [Swift](./Array/StrobogrammaticNumber.swift)| Easy| O(n)| O(1)|
5152
[Two Sum](https://leetcode.com/problems/two-sum/)| [Swift](./Array/TwoSum.swift)| Easy| O(n)| O(n)|
5253
[3Sum](https://leetcode.com/problems/3sum/)| [Swift](./Array/ThreeSum.swift)| Medium| O(n^2)| O(nC3)|
5354
[3Sum Closest](https://leetcode.com/problems/3sum-closest/)| [Swift](./Array/ThreeSum.swift)| Medium| O(n^2)| O(nC3)|
@@ -226,6 +227,7 @@
226227
[Combination Sum III](https://leetcode.com/problems/combination-sum-iii/)| [Swift](./DFS/CombinationSumIII.swift)| Medium| O(n!)| O(nCk)|
227228
[Letter Combinations of a Phone Number](https://leetcode.com/problems/letter-combinations-of-a-phone-number/)| [Swift](./DFS/LetterCombinationsPhoneNumber.swift)| Medium| O(mn)| O(n)|
228229
[Factor Combinations](https://leetcode.com/problems/factor-combinations/)| [Swift](./DFS/FactorCombinations.swift)| Medium| O(n^n))| O(2^n - 1)|
230+
[Strobogrammatic Number II](https://leetcode.com/problems/strobogrammatic-number-ii/)| [Swift](./DFS/StrobogrammaticNumberII.swift)| Medium| O(m^n)| O(n)|
229231
[Generalized Abbreviation](https://leetcode.com/problems/generalized-abbreviation/)| [Swift](./DFS/GeneralizedAbbreviation.swift)| Medium| O(n!)| O(2^n)|
230232
[Palindrome Partitioning](https://leetcode.com/problems/palindrome-partitioning/)| [Swift](./DFS/PalindromePartitioning.swift)| Medium| O(n!)| O(n)|
231233
[Is Graph Bipartite](https://leetcode.com/problems/is-graph-bipartite/)| [Swift](./DFS/IsGraphBipartite.swift)| Medium| O(n)| O(n)|
@@ -580,8 +582,8 @@
580582
| | 250 | [Count Univalue Subtrees](https://leetcode.com/problems/count-univalue-subtrees/) &hearts; | Medium |
581583
| | 249 | [Group Shifted Strings](https://leetcode.com/problems/group-shifted-strings/) &hearts; | Easy |
582584
| | 248 | [Strobogrammatic Number III](https://leetcode.com/problems/strobogrammatic-number-iii/) &hearts; | Hard |
583-
| | 247 | [Strobogrammatic Number II](https://leetcode.com/problems/strobogrammatic-number-ii/) &hearts; | Medium |
584-
| | 246 | [Strobogrammatic Number](https://leetcode.com/problems/strobogrammatic-number/) &hearts; | Easy |
585+
| [Swift](./DFS/StrobogrammaticNumberII.swift) | 247 | [Strobogrammatic Number II](https://leetcode.com/problems/strobogrammatic-number-ii/) &hearts; | Medium |
586+
| [Swift](./Array/StrobogrammaticNumber.swift) | 246 | [Strobogrammatic Number](https://leetcode.com/problems/strobogrammatic-number/) &hearts; | Easy |
585587
| [Swift](./Array/ShortestWordDistanceIII.swift) | 245 | [Shortest Word Distance III](https://leetcode.com/problems/shortest-word-distance-iii/) &hearts; | Medium |
586588
| | 244 | [Shortest Word Distance II](https://leetcode.com/problems/shortest-word-distance-ii/) &hearts; | Medium |
587589
| [Swift](./String/ShortestWordDistance.swift) | 243 | [Shortest Word Distance](https://leetcode.com/problems/shortest-word-distance/) &hearts; | Easy |

0 commit comments

Comments
 (0)