|
53 | 53 |
|
54 | 54 | <!-- 这里可写通用的实现逻辑 -->
|
55 | 55 |
|
| 56 | +**方法一:贪心 + 双指针** |
| 57 | + |
56 | 58 | <!-- tabs:start -->
|
57 | 59 |
|
58 | 60 | ### **Python3**
|
59 | 61 |
|
60 | 62 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
61 | 63 |
|
62 | 64 | ```python
|
63 |
| - |
| 65 | +class Solution: |
| 66 | + def repeatLimitedString(self, s: str, repeatLimit: int) -> str: |
| 67 | + cnt = [0] * 26 |
| 68 | + for c in s: |
| 69 | + cnt[ord(c) - ord('a')] += 1 |
| 70 | + ans = [] |
| 71 | + for i in range(25, -1, -1): |
| 72 | + j = i - 1 |
| 73 | + while 1: |
| 74 | + for _ in range(min(repeatLimit, cnt[i])): |
| 75 | + cnt[i] -= 1 |
| 76 | + ans.append(chr(ord('a') + i)) |
| 77 | + if cnt[i] == 0: |
| 78 | + break |
| 79 | + while j >= 0 and cnt[j] == 0: |
| 80 | + j -= 1 |
| 81 | + if j < 0: |
| 82 | + break |
| 83 | + cnt[j] -= 1 |
| 84 | + ans.append(chr(ord('a') + j)) |
| 85 | + return ''.join(ans) |
64 | 86 | ```
|
65 | 87 |
|
66 | 88 | ### **Java**
|
67 | 89 |
|
68 | 90 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
69 | 91 |
|
70 | 92 | ```java
|
| 93 | +class Solution { |
| 94 | + public String repeatLimitedString(String s, int repeatLimit) { |
| 95 | + int[] cnt = new int[26]; |
| 96 | + for (char c : s.toCharArray()) { |
| 97 | + cnt[c - 'a']++; |
| 98 | + } |
| 99 | + StringBuilder ans = new StringBuilder(); |
| 100 | + for (int i = 25; i >= 0; --i) { |
| 101 | + int j = i - 1; |
| 102 | + while (true) { |
| 103 | + for (int k = Math.min(repeatLimit, cnt[i]); k > 0; --k) { |
| 104 | + cnt[i]--; |
| 105 | + ans.append((char) ('a' + i)); |
| 106 | + } |
| 107 | + if (cnt[i] == 0) { |
| 108 | + break; |
| 109 | + } |
| 110 | + while (j >= 0 && cnt[j] == 0) { |
| 111 | + --j; |
| 112 | + } |
| 113 | + if (j < 0) { |
| 114 | + break; |
| 115 | + } |
| 116 | + cnt[j]--; |
| 117 | + ans.append((char) ('a' + j)); |
| 118 | + } |
| 119 | + } |
| 120 | + return ans.toString(); |
| 121 | + } |
| 122 | +} |
| 123 | +``` |
| 124 | + |
| 125 | +### **C++** |
| 126 | + |
| 127 | +```cpp |
| 128 | +class Solution { |
| 129 | +public: |
| 130 | + string repeatLimitedString(string s, int repeatLimit) { |
| 131 | + vector<int> cnt(26); |
| 132 | + for (char& c : s) cnt[c - 'a']++; |
| 133 | + string ans; |
| 134 | + for (int i = 25; ~i; --i) |
| 135 | + { |
| 136 | + int j = i - 1; |
| 137 | + while (true) |
| 138 | + { |
| 139 | + for (int k = min(cnt[i], repeatLimit); k; --k) |
| 140 | + { |
| 141 | + cnt[i]--; |
| 142 | + ans.push_back('a' + i); |
| 143 | + } |
| 144 | + if (cnt[i] == 0) break; |
| 145 | + while (~j && cnt[j] == 0) --j; |
| 146 | + if (j < 0) break; |
| 147 | + cnt[j]--; |
| 148 | + ans.push_back('a' + j); |
| 149 | + } |
| 150 | + } |
| 151 | + return ans; |
| 152 | + } |
| 153 | +}; |
| 154 | +``` |
71 | 155 |
|
| 156 | +### **Go** |
| 157 | +
|
| 158 | +```go |
| 159 | +func repeatLimitedString(s string, repeatLimit int) string { |
| 160 | + cnt := make([]int, 26) |
| 161 | + for _, c := range s { |
| 162 | + cnt[c-'a']++ |
| 163 | + } |
| 164 | + var ans []byte |
| 165 | + for i := 25; i >= 0; i-- { |
| 166 | + j := i - 1 |
| 167 | + for { |
| 168 | + for k := min(cnt[i], repeatLimit); k > 0; k-- { |
| 169 | + cnt[i]-- |
| 170 | + ans = append(ans, 'a'+byte(i)) |
| 171 | + } |
| 172 | + if cnt[i] == 0 { |
| 173 | + break |
| 174 | + } |
| 175 | + for j >= 0 && cnt[j] == 0 { |
| 176 | + j-- |
| 177 | + } |
| 178 | + if j < 0 { |
| 179 | + break |
| 180 | + } |
| 181 | + cnt[j]-- |
| 182 | + ans = append(ans, 'a'+byte(j)) |
| 183 | + } |
| 184 | + } |
| 185 | + return string(ans) |
| 186 | +} |
| 187 | +
|
| 188 | +func min(a, b int) int { |
| 189 | + if a < b { |
| 190 | + return a |
| 191 | + } |
| 192 | + return b |
| 193 | +} |
72 | 194 | ```
|
73 | 195 |
|
74 | 196 | ### **TypeScript**
|
|
0 commit comments