Skip to content

feat: add solutions to lc problem: No.3167 #2956

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -208,22 +208,22 @@ public class Solution {
class Solution {
func singleNumber(_ nums: [Int]) -> Int {
var bitCounts = [Int](repeating: 0, count: 32)

for num in nums {
var x = num
for i in 0..<32 {
bitCounts[i] += x & 1
x >>= 1
}
}

var result = 0
for i in 0..<32 {
if bitCounts[i] % 3 == 1 {
result |= 1 << i
}
}

return result
}
}
Expand Down
4 changes: 2 additions & 2 deletions lcof/面试题57 - II. 和为s的连续正数序列/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ class Solution {
func findContinuousSequence(_ target: Int) -> [[Int]] {
var l = 1, r = 2
var result = [[Int]]()

while l < r {
let sum = (l + r) * (r - l + 1) / 2
if sum == target {
Expand All @@ -234,7 +234,7 @@ class Solution {
l += 1
}
}

return result
}
}
Expand Down
4 changes: 2 additions & 2 deletions lcof/面试题57. 和为s的两个数字/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ class Solution {
func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
var l = 0
var r = nums.count - 1

while l < r {
let sum = nums[l] + nums[r]
if sum == target {
Expand All @@ -230,7 +230,7 @@ class Solution {
l += 1
}
}

return []
}
}
Expand Down
4 changes: 2 additions & 2 deletions lcof/面试题58 - I. 翻转单词顺序/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ class Solution {
func reverseWords(_ s: String) -> String {
var words = [String]()
var i = s.startIndex

while i < s.endIndex {
while i < s.endIndex && s[i] == " " {
i = s.index(after: i)
Expand All @@ -274,7 +274,7 @@ class Solution {
i = j
}
}

words.reverse()
return words.joined(separator: " ")
}
Expand Down
10 changes: 5 additions & 5 deletions lcof/面试题59 - I. 滑动窗口的最大值/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -262,23 +262,23 @@ class Solution {
let n = nums.count
var ans = [Int]()
var deque = [Int]()

for i in 0..<n {
if !deque.isEmpty && deque.first! < i - k + 1 {
deque.removeFirst()
}

while !deque.isEmpty && nums[deque.last!] <= nums[i] {
deque.removeLast()
}

deque.append(i)

if i >= k - 1 {
ans.append(nums[deque.first!])
}
}

return ans
}
}
Expand Down
8 changes: 4 additions & 4 deletions lcof/面试题59 - II. 队列的最大值/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -428,22 +428,22 @@ public class MaxQueue {
class MaxQueue {
private var q1: [Int] = []
private var q2: [Int] = []

init() {
}

func max_value() -> Int {
return q2.isEmpty ? -1 : q2.first!
}

func push_back(_ value: Int) {
q1.append(value)
while !q2.isEmpty && q2.last! < value {
q2.removeLast()
}
q2.append(value)
}

func pop_front() -> Int {
if q1.isEmpty {
return -1
Expand Down
220 changes: 220 additions & 0 deletions solution/3100-3199/3167.Better Compression of String/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
---
comments: true
difficulty: 中等
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3167.Better%20Compression%20of%20String/README.md
---

<!-- problem:start -->

# [3167. Better Compression of String 🔒](https://leetcode.cn/problems/better-compression-of-string)

[English Version](/solution/3100-3199/3167.Better%20Compression%20of%20String/README_EN.md)

## 题目描述

<!-- description:start -->

<p>You are given a string <code>compressed</code> representing a compressed version of a string. The format is a character followed by its frequency. For example, <code>&quot;a3b1a1c2&quot;</code> is a compressed version of the string <code>&quot;aaabacc&quot;</code>.</p>

<p>We seek a <strong>better compression</strong> with the following conditions:</p>

<ol>
<li>Each character should appear <strong>only once</strong> in the compressed version.</li>
<li>The characters should be in <strong>alphabetical order</strong>.</li>
</ol>

<p>Return the <em>better compression</em> of <code>compressed</code>.</p>

<p><strong>Note:</strong> In the better version of compression, the order of letters may change, which is acceptable.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">compressed = &quot;a3c9b2c1&quot;</span></p>

<p><strong>Output:</strong> <span class="example-io">&quot;a3b2c10&quot;</span></p>

<p><strong>Explanation:</strong></p>

<p>Characters &quot;a&quot; and &quot;b&quot; appear only once in the input, but &quot;c&quot; appears twice, once with a size of 9 and once with a size of 1.</p>

<p>Hence, in the resulting string, it should have a size of 10.</p>
</div>

<p><strong class="example">Example 2:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">compressed = &quot;c2b3a1&quot;</span></p>

<p><strong>Output:</strong> <span class="example-io">&quot;a1b3c2&quot;</span></p>
</div>

<p><strong class="example">Example 3:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">compressed = &quot;a2b4c1&quot;</span></p>

<p><strong>Output:</strong> <span class="example-io">&quot;a2b4c1&quot;</span></p>
</div>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>1 &lt;= compressed.length &lt;= 6 * 10<sup>4</sup></code></li>
<li><code>compressed</code> consists only of lowercase English letters and digits.</li>
<li><code>compressed</code> is a valid compression, i.e., each character is followed by its frequency.</li>
<li>Frequencies are in the range <code>[1, 10<sup>4</sup>]</code> and have no leading zeroes.</li>
</ul>

<!-- description:end -->

## 解法

<!-- solution:start -->

### 方法一:哈希表 + 双指针

我们可以使用哈希表来统计每个字符的频率,然后使用双指针来遍历 `compressed` 字符串,将每个字符的频率累加到哈希表中,最后按照字母顺序将字符和频率拼接成字符串。

时间复杂度 $O(n + |\Sigma| \log |\Sigma|)$,空间复杂度 $O(|\Sigma|)$,其中 $n$ 是字符串 `compressed` 的长度,而 $|\Sigma|$ 是字符集的大小,这里字符集是小写字母,所以 $|\Sigma| = 26$。

<!-- tabs:start -->

#### Python3

```python
class Solution:
def betterCompression(self, compressed: str) -> str:
cnt = Counter()
i, n = 0, len(compressed)
while i < n:
j = i + 1
x = 0
while j < n and compressed[j].isdigit():
x = x * 10 + int(compressed[j])
j += 1
cnt[compressed[i]] += x
i = j
return "".join(sorted(f"{k}{v}" for k, v in cnt.items()))
```

#### Java

```java
class Solution {
public String betterCompression(String compressed) {
Map<Character, Integer> cnt = new TreeMap<>();
int i = 0;
int n = compressed.length();
while (i < n) {
char c = compressed.charAt(i);
int j = i + 1;
int x = 0;
while (j < n && Character.isDigit(compressed.charAt(j))) {
x = x * 10 + (compressed.charAt(j) - '0');
j++;
}
cnt.merge(c, x, Integer::sum);
i = j;
}
StringBuilder ans = new StringBuilder();
for (var e : cnt.entrySet()) {
ans.append(e.getKey()).append(e.getValue());
}
return ans.toString();
}
}
```

#### C++

```cpp
class Solution {
public:
string betterCompression(string compressed) {
map<char, int> cnt;
int i = 0;
int n = compressed.length();
while (i < n) {
char c = compressed[i];
int j = i + 1;
int x = 0;
while (j < n && isdigit(compressed[j])) {
x = x * 10 + (compressed[j] - '0');
j++;
}
cnt[c] += x;
i = j;
}
stringstream ans;
for (const auto& entry : cnt) {
ans << entry.first << entry.second;
}
return ans.str();
}
};
```

#### Go

```go
func betterCompression(compressed string) string {
cnt := map[byte]int{}
n := len(compressed)
for i := 0; i < n; {
c := compressed[i]
j := i + 1
x := 0
for j < n && compressed[j] >= '0' && compressed[j] <= '9' {
x = x*10 + int(compressed[j]-'0')
j++
}
cnt[c] += x
i = j
}
ans := strings.Builder{}
for c := byte('a'); c <= byte('z'); c++ {
if cnt[c] > 0 {
ans.WriteByte(c)
ans.WriteString(strconv.Itoa(cnt[c]))
}
}
return ans.String()
}
```

#### TypeScript

```ts
function betterCompression(compressed: string): string {
const cnt = new Map<string, number>();
const n = compressed.length;
let i = 0;

while (i < n) {
const c = compressed[i];
let j = i + 1;
let x = 0;
while (j < n && /\d/.test(compressed[j])) {
x = x * 10 + +compressed[j];
j++;
}
cnt.set(c, (cnt.get(c) || 0) + x);
i = j;
}
const keys = Array.from(cnt.keys()).sort();
const ans: string[] = [];
for (const k of keys) {
ans.push(`${k}${cnt.get(k)}`);
}
return ans.join('');
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Loading
Loading