Skip to content

Latest commit

 

History

History
38 lines (28 loc) · 778 Bytes

1400. 构造 K 个回文字符串.md

File metadata and controls

38 lines (28 loc) · 778 Bytes
  • 哈希表(碰巧跟题解思路一模一样)
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;
};