判断短字符串S中的所有字符是否在长字符串T中全部出现。
数据范围: 1≤len(S),len(T)≤200
进阶:时间复杂度:O(n) ,空间复杂度:O(n)
输入描述:输入两个字符串。第一个为短字符串,第二个为长字符串。两个字符串均由小写字母组成。
输出描述:如果短字符串的所有字符均在长字符串中出现过,则输出字符串"true"。否则输出字符串"false"。
输入:bc
abc
输出:true
说明:其中abc含有bc,输出"true"
#include<stdio.h>
#include<string.h>
int main() {
char str1[200];
char str2[200];
int cnt=0,isStr = 0;
int hash[200]={0};
scanf("%s", str1); //短字符
scanf("%s", str2);
for (int i = 0; i < strlen(str2); i++) {
hash[str2[i]] = 1; //将字符串字符所在位置赋值为1;
}
for (int i = 0; i < strlen(str1); i++) {
if(hash[str1[i]]){
cnt++;//统计短字符所在位置的个数
}
}
if(cnt == strlen(str1)){
isStr=1;
}
if (isStr) {
printf("true\n");
} else {
printf("false\n");
}
return 0;
}