- 哈希表(碰巧跟题解思路一模一样)
function canConstruct(s: string, k: number): boolean {
if (k === 0 || k === s.length) {
return true;
}
if (k > s.length) {
return false;
}
const map: Map<string, number> = new Map<string, number>();
for (const ch of s) {
map.set(ch, (map.get(ch) || 0) + 1);
}
let oddTimes: number = 0,
evenTimes: number = 0;
for (const [key, value] of map) {
if (value & 1) {
oddTimes++;
}
}
const [minBound, maxBound] = [
oddTimes,
s.length
];
return minBound <= k && k <= maxBound;
};