Skip to content

Commit edf80cc

Browse files
committed
[String] Add a solution to String Compression
1 parent 10a90cd commit edf80cc

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-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 257 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 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).
3232

3333

3434
## Array
@@ -94,6 +94,7 @@
9494
[Reverse String II](https://leetcode.com/problems/reverse-string-ii/)| [Swift](./String/ReverseStringII.swift)| Easy| O(n)| O(n)|
9595
[Reverse Vowels of a String](https://leetcode.com/problems/reverse-vowels-of-a-string/)| [Swift](./String/ReverseVowelsOfAString.swift)| Easy| O(n)| O(n)|
9696
[Length of Last Word](https://leetcode.com/problems/length-of-last-word/)| [Swift](./String/AddStrings.swift)| Easy| O(n)| O(n)|
97+
[String Compression](https://leetcode.com/problems/string-compression/)| [Swift](./String/StringCompression.swift)| Easy| O(n)| O(1)|
9798
[Add Strings](https://leetcode.com/problems/add-strings/)| [Swift](./String/LengthLastWord.swift)| Easy| O(n)| O(1)|
9899
[Multiply Strings](https://leetcode.com/problems/multiply-strings/)| [Swift](./String/MultiplyStrings.swift)| Medium| O(n)| O(1)|
99100
[Palindrome Permutation](https://leetcode.com/problems/palindrome-permutation/)| [Swift](./String/PalindromePermutation.swift)| Easy| O(n)| O(n)|

String/StringCompression.swift

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/string-compression/
3+
* Primary idea: Compare two consecutive characters, set character at index and count of it afterwards
4+
*
5+
* Note: do not forget to handle edge case when hit the end of array
6+
*
7+
* Time Complexity: O(n), Space Complexity: O(1)
8+
*
9+
*/
10+
11+
class StringCompression {
12+
func compress(_ chars: inout [Character]) -> Int {
13+
var index = 0, currentCount = 0
14+
15+
for i in 0..<chars.count {
16+
currentCount += 1
17+
18+
if i + 1 == chars.count || chars[i] != chars[i + 1] {
19+
chars[index] = chars[i]
20+
21+
if currentCount != 1 {
22+
chars.replaceSubrange(index + 1...currentCount.length + index, with: Array(String(currentCount)))
23+
}
24+
25+
index += currentCount == 1 ? 1 : 1 + currentCount.length
26+
currentCount = 0
27+
}
28+
}
29+
30+
return index
31+
}
32+
}
33+
34+
extension Int {
35+
var length: Int {
36+
return String(self).count
37+
}
38+
}

0 commit comments

Comments
 (0)