Skip to content

Commit

Permalink
[String] Add a solution to Shortest Distance to a Character
Browse files Browse the repository at this point in the history
  • Loading branch information
soapyigu committed Jul 1, 2018
1 parent bef1316 commit 5fa1383
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* [Microsoft](#microsoft)

## Progress
[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).
[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).


## Array
Expand Down Expand Up @@ -96,6 +96,7 @@
[Length of Last Word](https://leetcode.com/problems/length-of-last-word/)| [Swift](./String/AddStrings.swift)| Easy| O(n)| O(n)|
[String Compression](https://leetcode.com/problems/string-compression/)| [Swift](./String/StringCompression.swift)| Easy| O(n)| O(1)|
[Add Strings](https://leetcode.com/problems/add-strings/)| [Swift](./String/LengthLastWord.swift)| Easy| O(n)| O(1)|
[Shortest Distance to a Character](https://leetcode.com/problems/shortest-distance-to-a-character/)| [Swift](./String/ShortestDistanceToACharacter.swift)| Easy| O(n)| O(1)|
[Multiply Strings](https://leetcode.com/problems/multiply-strings/)| [Swift](./String/MultiplyStrings.swift)| Medium| O(n)| O(1)|
[Palindrome Permutation](https://leetcode.com/problems/palindrome-permutation/)| [Swift](./String/PalindromePermutation.swift)| Easy| O(n)| O(n)|
[Valid Anagram](https://leetcode.com/problems/valid-anagram/)| [Swift](./String/ValidAnagram.swift)| Easy| O(nlogn)| O(1)|
Expand Down
35 changes: 35 additions & 0 deletions String/ShortestDistanceToACharacter.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Question Link: https://leetcode.com/problems/shortest-distance-to-a-character/
* Primary idea: Iterate through left and right to get min distance by compared between indices of C at two sides.
*
* Time Complexity: O(n), Space Complexity: O(1)
*
*/

class ShortestDistanceToACharacter {
func shortestToChar(_ S: String, _ C: Character) -> [Int] {
var res = Array(repeating: 0, count: S.count), cIndex = -10000, sChars = Array(S)

for (i, sChar) in sChars.enumerated() {
if sChar == C {
cIndex = i
}

res[i] = i - cIndex
}

cIndex = -10000

for i in (0..<sChars.count).reversed() {
let currentChar = sChars[i]

if currentChar == C {
cIndex = i
}

res[i] = min(res[i], abs(cIndex - i))
}

return res
}
}

0 comments on commit 5fa1383

Please sign in to comment.