Medium
You are given two strings s and t of the same length, consisting of uppercase English letters. Your task is to find the minimum number of "replacement operations" needed to get some anagram of the string t from the string s. A replacement operation is performed by picking exactly one character from the string s and replacing it by some other character.
Example
- For s = "AABAA" and t = "BBAAA", the output should be
createAnagram(s, t) = 1; - For s = "OVGHK" and t = "RPGUC", the output should be
createAnagram(s, t) = 4.
Input/Output
-
[execution time limit] 0.5 seconds (c)
-
[input] string s
Guaranteed constraints:
5 ≤ s.length ≤ 35. -
[input] string t
Guaranteed constraints:
t.length = s.length. -
[output] integer
- The minimum number of replacement operations needed to get an anagram of the string t from the string s.
[C] Syntax Tips
// Prints help message to the console
// Returns a string
char * helloWorld(char * name) {
char * answer = malloc(strlen(name) + 8);
printf("This prints to the console when you Run Tests");
strcpy(answer, "Hello, ");
strcat(answer, name);
return answer;
}
더보기
Solution
int createAnagram(char *s,char *t)
{
int alphabet_s[26]={0, }, alphabet_t[26]={0, }, count=0;
for(int i=0;i<strlen(s);i++)
{
alphabet_s[s[i]-'A']++;
alphabet_t[t[i]-'A']++;
}
for(int i=0;i<26;i++)
count+=abs(alphabet_s[i]-alphabet_t[i]);
return count/2;
}
728x90
'Codesignal' 카테고리의 다른 글
<Codesignal> Numbers Grouping (0) | 2020.05.02 |
---|---|
<Codesignal> Most Frequent Digit Sum (0) | 2020.04.30 |
<Codesignal> Is Substitution Cipher? (0) | 2020.04.19 |
<Codesignal> Strings Construction (0) | 2020.04.19 |
<Codesignal> HTML End Tag By Start Tag (0) | 2020.04.19 |