Skip to content

Commit 5fa1383

Browse files
committed
[String] Add a solution to Shortest Distance to a Character
1 parent bef1316 commit 5fa1383

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

README.md

+2-1
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 258 completed solutions. Note: questions with ♥ 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 259 completed solutions. Note: questions with ♥ 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
@@ -96,6 +96,7 @@
9696
[Length of Last Word](https://leetcode.com/problems/length-of-last-word/)| [Swift](./String/AddStrings.swift)| Easy| O(n)| O(n)|
9797
[String Compression](https://leetcode.com/problems/string-compression/)| [Swift](./String/StringCompression.swift)| Easy| O(n)| O(1)|
9898
[Add Strings](https://leetcode.com/problems/add-strings/)| [Swift](./String/LengthLastWord.swift)| Easy| O(n)| O(1)|
99+
[Shortest Distance to a Character](https://leetcode.com/problems/shortest-distance-to-a-character/)| [Swift](./String/ShortestDistanceToACharacter.swift)| Easy| O(n)| O(1)|
99100
[Multiply Strings](https://leetcode.com/problems/multiply-strings/)| [Swift](./String/MultiplyStrings.swift)| Medium| O(n)| O(1)|
100101
[Palindrome Permutation](https://leetcode.com/problems/palindrome-permutation/)| [Swift](./String/PalindromePermutation.swift)| Easy| O(n)| O(n)|
101102
[Valid Anagram](https://leetcode.com/problems/valid-anagram/)| [Swift](./String/ValidAnagram.swift)| Easy| O(nlogn)| O(1)|
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/shortest-distance-to-a-character/
3+
* Primary idea: Iterate through left and right to get min distance by compared between indices of C at two sides.
4+
*
5+
* Time Complexity: O(n), Space Complexity: O(1)
6+
*
7+
*/
8+
9+
class ShortestDistanceToACharacter {
10+
func shortestToChar(_ S: String, _ C: Character) -> [Int] {
11+
var res = Array(repeating: 0, count: S.count), cIndex = -10000, sChars = Array(S)
12+
13+
for (i, sChar) in sChars.enumerated() {
14+
if sChar == C {
15+
cIndex = i
16+
}
17+
18+
res[i] = i - cIndex
19+
}
20+
21+
cIndex = -10000
22+
23+
for i in (0..<sChars.count).reversed() {
24+
let currentChar = sChars[i]
25+
26+
if currentChar == C {
27+
cIndex = i
28+
}
29+
30+
res[i] = min(res[i], abs(cIndex - i))
31+
}
32+
33+
return res
34+
}
35+
}

0 commit comments

Comments
 (0)