Medium
Given two strings a and b, both consisting only of lowercase English letters, your task is to calculate how many strings equal to a can be constructed using only letters from the string b? Each letter can be used only once and in one string only.
Example
-
For a = "abc" and b = "abccba", the output should be stringsConstruction(a, b) = 2.
We can construct 2 strings a = "abc" using only letters from the string b.
-
For a = "ab" and b = "abcbcb", the output should be stringsConstruction(a, b) = 1.
Input/Output
-
[execution time limit] 0.5 seconds (c)
-
[input] string a
String to construct, containing only lowercase English letters.
Guaranteed constraints:
1 ≤ a.length ≤ 10^5. -
[input] string b
String containing needed letters, containing only lowercase English letters.
Guaranteed constraints:
1 ≤ b.length ≤ 10^5. -
[output] integer
- The number of strings a that can be constructed from the string b.
[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 stringsConstruction(char *a,char *b)
{
long long int alphabet_a[26]={0, }, alphabet_b[26]={0, }, min=1000000;
for(int i=0;i<strlen(a);i++)
alphabet_a[a[i]-'a']++;
for(int i=0;i<strlen(b);i++)
if(alphabet_a[b[i]-'a']!=0)
alphabet_b[b[i]-'a']++;
for(int i=0;i<26;i++)
if(alphabet_a[i]!=0)
min=alphabet_b[i]/alphabet_a[i]<min?alphabet_b[i]/alphabet_a[i]:min;
return min;
}
'Codesignal' 카테고리의 다른 글
<Codesignal> Create Anagram (0) | 2020.04.19 |
---|---|
<Codesignal> Is Substitution Cipher? (0) | 2020.04.19 |
<Codesignal> HTML End Tag By Start Tag (0) | 2020.04.19 |
<Codesignal> Find Email Domain (0) | 2020.04.19 |
<Codesignal> Is Case-Insensitive Palindrome? (0) | 2020.04.19 |