diff options
author | Jack Sangdahl <[email protected]> | 2025-01-10 20:31:27 -0500 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2025-01-10 20:31:27 -0500 |
commit | 86fae540ca9b40b8294541593b32600549c5a653 (patch) | |
tree | 8e30111147c6fa91ce33f318e681511f928951da | |
parent | 906231fba337f2e2a04f5ce33dff6c9cd43b7d97 (diff) | |
download | leetcode-86fae540ca9b40b8294541593b32600549c5a653.tar.gz leetcode-86fae540ca9b40b8294541593b32600549c5a653.zip |
1400. construct k palindrome strings
-rw-r--r-- | construct_k_palindrome_str.c | 20 |
1 files changed, 7 insertions, 13 deletions
diff --git a/construct_k_palindrome_str.c b/construct_k_palindrome_str.c index 843ebbb..9dadb08 100644 --- a/construct_k_palindrome_str.c +++ b/construct_k_palindrome_str.c @@ -7,20 +7,14 @@ */ bool canConstruct(char *s, int k) { - int n = strlen(s); - if (n < k) - return 0; - int *c = calloc(26, sizeof(int)); - for (int i = 0; i < n; i++) - c[s[i] - 'a']++; - int odd = 0; + int cnt[26] = {0}, odds = 0; + if (k > strlen(s)) + return false; + for (int i = 0; s[i]; i++) + cnt[s[i] - 'a']++; for (int i = 0; i < 26; i++) - if (c[i] % 2 == 1) - odd++; - free(c); - if (odd > k) - return 0; - return 1; + odds += (cnt[i] % 2); + return k >= odds; } int main() { |