计算赎金信
给你两个字符串:ransomNote
和 magazine
,判断 ransomNote
能不能由 magazine
里面的字符构成。如果可以,返回 true
;否则返回 false
。magazine
中的每个字符只能在 ransomNote
中使用一次。
示例 1:
输入:ransomNote = "a", magazine = "b" 输出:false
示例 2:
输入:ransomNote = "aa", magazine = "ab" 输出:false
示例 3:
输入:ransomNote = "aa", magazine = "aab" 输出:true
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
bool canConstruct(char* ransomNote, char* magazine)
{
int len1 = strlen(ransomNote);
int len2 = strlen(magazine);
if(len1 > len2)
{
return false;
}
int start = 0;
int end = 0;
for(int j = 0;j < len2;j++)
{
int s = j;
while(ransomNote[start] == magazine[j] && ransomNote[start] != '\0')
{
start++;
j++;
end++;
}
if(end == len1)
{
return true;
}else
{
start = 0;
end = 0;
j = s;
}
}
return false;
}
int main()
{
char ransomNote[] = "aa";
char magazine[] = "bbabaa";
int a = canConstruct(ransomNote,magazine);
printf("%d\n",a);
return 0;
}