diff --git a/solution/3100-3199/3163.String Compression III/README.md b/solution/3100-3199/3163.String Compression III/README.md index 9a19611970358..397e81726948c 100644 --- a/solution/3100-3199/3163.String Compression III/README.md +++ b/solution/3100-3199/3163.String Compression III/README.md @@ -80,32 +80,129 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3163.St -### 方法一 +### 方法一:双指针 + +我们可以利用双指针,统计每个字符的连续出现次数。假如当前字符 $c$ 连续出现了 $k$ 次,然后我们将 $k$ 划分成若干个 $x$,每个 $x$ 最大为 $9$,然后将 $x$ 和 $c$ 拼接起来,将每个 $x$ 和 $c$ 拼接起来到结果中。 + +最后返回结果即可。 + +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为字符串 `word` 的长度。 #### Python3 ```python - +class Solution: + def compressedString(self, word: str) -> str: + g = groupby(word) + ans = [] + for c, v in g: + k = len(list(v)) + while k: + x = min(9, k) + ans.append(str(x) + c) + k -= x + return "".join(ans) ``` #### Java ```java - +class Solution { + public String compressedString(String word) { + StringBuilder ans = new StringBuilder(); + int n = word.length(); + for (int i = 0; i < n;) { + int j = i + 1; + while (j < n && word.charAt(j) == word.charAt(i)) { + ++j; + } + int k = j - i; + while (k > 0) { + int x = Math.min(9, k); + ans.append(x).append(word.charAt(i)); + k -= x; + } + i = j; + } + return ans.toString(); + } +} ``` #### C++ ```cpp - +class Solution { +public: + string compressedString(string word) { + string ans; + int n = word.length(); + for (int i = 0; i < n;) { + int j = i + 1; + while (j < n && word[j] == word[i]) { + ++j; + } + int k = j - i; + while (k > 0) { + int x = min(9, k); + ans.push_back('0' + x); + ans.push_back(word[i]); + k -= x; + } + i = j; + } + return ans; + } +}; ``` #### Go ```go +func compressedString(word string) string { + ans := []byte{} + n := len(word) + for i := 0; i < n; { + j := i + 1 + for j < n && word[j] == word[i] { + j++ + } + k := j - i + for k > 0 { + x := min(9, k) + ans = append(ans, byte('0'+x)) + ans = append(ans, word[i]) + k -= x + } + i = j + } + return string(ans) +} +``` +#### TypeScript + +```ts +function compressedString(word: string): string { + const ans: string[] = []; + const n = word.length; + for (let i = 0; i < n; ) { + let j = i + 1; + while (j < n && word[j] === word[i]) { + ++j; + } + let k = j - i; + while (k) { + const x = Math.min(k, 9); + ans.push(x + word[i]); + k -= x; + } + i = j; + } + return ans.join(''); +} ``` diff --git a/solution/3100-3199/3163.String Compression III/README_EN.md b/solution/3100-3199/3163.String Compression III/README_EN.md index abba9b4de8b5c..8363f6a129bee 100644 --- a/solution/3100-3199/3163.String Compression III/README_EN.md +++ b/solution/3100-3199/3163.String Compression III/README_EN.md @@ -76,32 +76,129 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3163.St -### Solution 1 +### Solution 1: Two Pointers + +We can use two pointers to count the consecutive occurrences of each character. Suppose the current character $c$ appears consecutively $k$ times, then we divide $k$ into several $x$, each $x$ is at most $9$, then we concatenate $x$ and $c$, and append each $x$ and $c$ to the result. + +Finally, return the result. + +The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the length of the #### Python3 ```python - +class Solution: + def compressedString(self, word: str) -> str: + g = groupby(word) + ans = [] + for c, v in g: + k = len(list(v)) + while k: + x = min(9, k) + ans.append(str(x) + c) + k -= x + return "".join(ans) ``` #### Java ```java - +class Solution { + public String compressedString(String word) { + StringBuilder ans = new StringBuilder(); + int n = word.length(); + for (int i = 0; i < n;) { + int j = i + 1; + while (j < n && word.charAt(j) == word.charAt(i)) { + ++j; + } + int k = j - i; + while (k > 0) { + int x = Math.min(9, k); + ans.append(x).append(word.charAt(i)); + k -= x; + } + i = j; + } + return ans.toString(); + } +} ``` #### C++ ```cpp - +class Solution { +public: + string compressedString(string word) { + string ans; + int n = word.length(); + for (int i = 0; i < n;) { + int j = i + 1; + while (j < n && word[j] == word[i]) { + ++j; + } + int k = j - i; + while (k > 0) { + int x = min(9, k); + ans.push_back('0' + x); + ans.push_back(word[i]); + k -= x; + } + i = j; + } + return ans; + } +}; ``` #### Go ```go +func compressedString(word string) string { + ans := []byte{} + n := len(word) + for i := 0; i < n; { + j := i + 1 + for j < n && word[j] == word[i] { + j++ + } + k := j - i + for k > 0 { + x := min(9, k) + ans = append(ans, byte('0'+x)) + ans = append(ans, word[i]) + k -= x + } + i = j + } + return string(ans) +} +``` +#### TypeScript + +```ts +function compressedString(word: string): string { + const ans: string[] = []; + const n = word.length; + for (let i = 0; i < n; ) { + let j = i + 1; + while (j < n && word[j] === word[i]) { + ++j; + } + let k = j - i; + while (k) { + const x = Math.min(k, 9); + ans.push(x + word[i]); + k -= x; + } + i = j; + } + return ans.join(''); +} ``` diff --git a/solution/3100-3199/3163.String Compression III/Solution.cpp b/solution/3100-3199/3163.String Compression III/Solution.cpp new file mode 100644 index 0000000000000..0729e854bd9c5 --- /dev/null +++ b/solution/3100-3199/3163.String Compression III/Solution.cpp @@ -0,0 +1,22 @@ +class Solution { +public: + string compressedString(string word) { + string ans; + int n = word.length(); + for (int i = 0; i < n;) { + int j = i + 1; + while (j < n && word[j] == word[i]) { + ++j; + } + int k = j - i; + while (k > 0) { + int x = min(9, k); + ans.push_back('0' + x); + ans.push_back(word[i]); + k -= x; + } + i = j; + } + return ans; + } +}; \ No newline at end of file diff --git a/solution/3100-3199/3163.String Compression III/Solution.go b/solution/3100-3199/3163.String Compression III/Solution.go new file mode 100644 index 0000000000000..334f51b327def --- /dev/null +++ b/solution/3100-3199/3163.String Compression III/Solution.go @@ -0,0 +1,19 @@ +func compressedString(word string) string { + ans := []byte{} + n := len(word) + for i := 0; i < n; { + j := i + 1 + for j < n && word[j] == word[i] { + j++ + } + k := j - i + for k > 0 { + x := min(9, k) + ans = append(ans, byte('0'+x)) + ans = append(ans, word[i]) + k -= x + } + i = j + } + return string(ans) +} \ No newline at end of file diff --git a/solution/3100-3199/3163.String Compression III/Solution.java b/solution/3100-3199/3163.String Compression III/Solution.java new file mode 100644 index 0000000000000..b89e6a18b5102 --- /dev/null +++ b/solution/3100-3199/3163.String Compression III/Solution.java @@ -0,0 +1,20 @@ +class Solution { + public String compressedString(String word) { + StringBuilder ans = new StringBuilder(); + int n = word.length(); + for (int i = 0; i < n;) { + int j = i + 1; + while (j < n && word.charAt(j) == word.charAt(i)) { + ++j; + } + int k = j - i; + while (k > 0) { + int x = Math.min(9, k); + ans.append(x).append(word.charAt(i)); + k -= x; + } + i = j; + } + return ans.toString(); + } +} \ No newline at end of file diff --git a/solution/3100-3199/3163.String Compression III/Solution.py b/solution/3100-3199/3163.String Compression III/Solution.py new file mode 100644 index 0000000000000..634ba6e7f4053 --- /dev/null +++ b/solution/3100-3199/3163.String Compression III/Solution.py @@ -0,0 +1,11 @@ +class Solution: + def compressedString(self, word: str) -> str: + g = groupby(word) + ans = [] + for c, v in g: + k = len(list(v)) + while k: + x = min(9, k) + ans.append(str(x) + c) + k -= x + return "".join(ans) diff --git a/solution/3100-3199/3163.String Compression III/Solution.ts b/solution/3100-3199/3163.String Compression III/Solution.ts new file mode 100644 index 0000000000000..c5c8e6702024f --- /dev/null +++ b/solution/3100-3199/3163.String Compression III/Solution.ts @@ -0,0 +1,18 @@ +function compressedString(word: string): string { + const ans: string[] = []; + const n = word.length; + for (let i = 0; i < n; ) { + let j = i + 1; + while (j < n && word[j] === word[i]) { + ++j; + } + let k = j - i; + while (k) { + const x = Math.min(k, 9); + ans.push(x + word[i]); + k -= x; + } + i = j; + } + return ans.join(''); +}