DDSA
Advertisement

1400. Construct K Palindrome Strings

Time: O(n)
Space: O(1)

Approach

Count characters with odd frequency; need at least that many palindromes; also need |s| >= k.

1400.cs
C#
// Approach: Count characters with odd frequency; need at least that many palindromes; also need |s| >= k.
// Time: O(n) Space: O(1)

public class Solution
{
    public bool CanConstruct(string s, int k)
    {
        // If |s| < k, we cannot construct k strings from the s.
        if (s.Length < k)
            return false;

        int[] count = new int[26];

        foreach (char c in s)
            count[c - 'a'] ^= 1;

        // If the number of letters that have odd counts > k, the minimum number of
        // palindromic strings we can construct is > k.
        return count.Count(c => c % 2 == 1) <= k;
    }
}
Advertisement
Was this solution helpful?