-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) |