Skip to content

Commit

Permalink
add: 删除字符使频率相同
Browse files Browse the repository at this point in the history
  • Loading branch information
yi-ge committed Apr 29, 2023
1 parent a56ac4f commit 7adf9a5
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
"aaab",
"aaabcbc",
"AABC",
"aazz",
"abca",
"abcabcabcabc",
"abcabcbb",
"abcabccdcdcdef",
"abcc",
"abcda",
"abcdc",
"abcdefg",
Expand Down Expand Up @@ -37,6 +39,7 @@
"eleetminicoworoep",
"emibcn",
"esac",
"Freqs",
"Gitpod",
"heapify",
"holasss",
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,10 @@ TypeScript / JavaScript 基础算法、数据结构练习,包含 LeetCode 或

### 其它

- [删除字符使频率相同](src/map/remove-letter-to-equalize-frequency.ts) [哈希表, 字符串, 计数]

- LeetCode 2423. 删除字符使频率相同 <https://leetcode.cn/problems/remove-letter-to-equalize-frequency>

- [仅执行一次字符串交换能否使两个字符串相等](src/map/check-if-one-string-swap-can-make-strings-equal.ts) [哈希表, 字符串, 计数]

- LeetCode 1790. 仅执行一次字符串交换能否使两个字符串相等 <https://leetcode.cn/problems/check-if-one-string-swap-can-make-strings-equal>
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 40 additions & 0 deletions src/map/remove-letter-to-equalize-frequency.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// 删除字符使频率相同
// https://leetcode.cn/problems/remove-letter-to-equalize-frequency
// INLINE ../../images/map/remove-letter-to-equalize-frequency.jpeg

export function equalFrequency (word: string): boolean {
const charCount = new Array(26).fill(0)
for (const c of word) {
charCount[c.charCodeAt(0) - 'a'.charCodeAt(0)]++
}
const freqCount = new Map()
for (const c of charCount) {
if (c > 0) {
freqCount.set(c, (freqCount.get(c) || 0) + 1)
}
}
for (const c of charCount) {
if (c == 0) {
continue
}
freqCount.set(c, freqCount.get(c) - 1)
if (freqCount.get(c) == 0) {
freqCount.delete(c)
}
if (c - 1 > 0) {
freqCount.set(c - 1, (freqCount.get(c - 1) || 0) + 1)
}
if (freqCount.size == 1) {
return true
}
if (c - 1 > 0) {
freqCount.set(c - 1, freqCount.get(c - 1) - 1)
if (freqCount.get(c - 1) == 0) {
freqCount.delete(c - 1)
}
}
/* istanbul ignore next */
freqCount.set(c, (freqCount.get(c) || 0) + 1)
}
return false
}
17 changes: 17 additions & 0 deletions test/map/remove-letter-to-equalize-frequency.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { equalFrequency } from '../../src/map/remove-letter-to-equalize-frequency'

test('删除字符使频率相同', () => {
// 示例 1:
// 输入:word = "abcc"
// 输出:true
// 解释:选择下标 3 并删除该字母,word 变成 "abc" 且每个字母出现频率都为 1 。
const word = 'abcc'
expect(equalFrequency(word)).toBeTruthy()

// 示例 2:
// 输入:word = "aazz"
// 输出:false
// 解释:我们必须删除一个字母,所以要么 "a" 的频率变为 1 且 "z" 的频率为 2 ,要么两个字母频率反过来。所以不可能让剩余所有字母出现频率相同。
const word2 = 'aazz'
expect(equalFrequency(word2)).toBeFalsy()
})

0 comments on commit 7adf9a5

Please sign in to comment.