diff options
author | jack <[email protected]> | 2024-06-14 13:13:15 -0600 |
---|---|---|
committer | jack <[email protected]> | 2024-06-14 13:13:15 -0600 |
commit | d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa (patch) | |
tree | 96df922dae2a5073d9a50ea70c7aae6aa37e3ebe /construct_k_palindrome_str.c | |
download | leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.tar.gz leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.zip |
initial
Diffstat (limited to 'construct_k_palindrome_str.c')
-rw-r--r-- | construct_k_palindrome_str.c | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/construct_k_palindrome_str.c b/construct_k_palindrome_str.c new file mode 100644 index 0000000..843ebbb --- /dev/null +++ b/construct_k_palindrome_str.c @@ -0,0 +1,31 @@ +// 1400. Construct K Palindrome Strings +#include "leetcode.h" + +/* + * given a string 's' and an integer 'k', return true if you can use all the + * characters in 's' to constuct 'k' palindrome strings or false otherwise. + */ + +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; + for (int i = 0; i < 26; i++) + if (c[i] % 2 == 1) + odd++; + free(c); + if (odd > k) + return 0; + return 1; +} + +int main() { + char *s1 = "annabelle", *s2 = "leetcode", *s3 = "true"; + printf("%d\n", canConstruct(s1, 2)); // expect: 1 + printf("%d\n", canConstruct(s2, 3)); // expect: 0 + printf("%d\n", canConstruct(s3, 4)); // expect: 1 +} |