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 /min_num_steps_anagram.c | |
download | leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.tar.gz leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.zip |
initial
Diffstat (limited to 'min_num_steps_anagram.c')
-rw-r--r-- | min_num_steps_anagram.c | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/min_num_steps_anagram.c b/min_num_steps_anagram.c new file mode 100644 index 0000000..211c932 --- /dev/null +++ b/min_num_steps_anagram.c @@ -0,0 +1,31 @@ +// 1347. Minimum Number of Steps to Make Two Strings Anagram +#include "leetcode.h" + +/* + * given two trings of the same length 's' and 't'. in one step you can choose + * any character of 't' and replace itwith another characters. return the + * minimum numberof steps to make 't' an anagram of 's'. an anagram of a string + * is a string that contains the same characters witha different ordering. + */ + +int minSteps(char *s, char *t) { + int *alpha = calloc(26, sizeof(int)); + for (int i = 0; i < strlen(s); i++) { + alpha[s[i] - 'a']++; + alpha[t[i] - 'a']--; + } + int ans = 0; + for (int i = 0; i < 26; i++) + if (alpha[i] > 0) + ans += alpha[i]; + return ans; +} + +int main() { + char *s1 = "bab", *t1 = "aba"; + char *s2 = "leetcode", *t2 = "practice"; + char *s3 = "anagram", *t3 = "mangaar"; + printf("%d\n", minSteps(s1, t1)); // expect: 1 + printf("%d\n", minSteps(s2, t2)); // expect: 5 + printf("%d\n", minSteps(s3, t3)); // expect: 0 +} |