Arrays 计算C中一致字符串的数量
所以这是leetcode的一个问题,我有一些问题。。我已经在leetcode的讨论部分看到了解决这个问题的代码,但是我想知道是否有人可以用我已经编写的一些代码来帮助我解决这个问题。。问题是这样的: 您将获得一个字符串Arrays 计算C中一致字符串的数量,arrays,c,string,Arrays,C,String,所以这是leetcode的一个问题,我有一些问题。。我已经在leetcode的讨论部分看到了解决这个问题的代码,但是我想知道是否有人可以用我已经编写的一些代码来帮助我解决这个问题。。问题是这样的: 您将获得一个字符串allowed,该字符串由不同的字符和 字符串数组单词。如果字符串中的所有字符 该字符串出现在允许的字符串中。返回 数组字中的一致字符串 示例: 输入:allowed=“ab”,words=>[“ad”、“bd”、“aaab”、“baa”、“badab”] 产出:2 说明:字符串“a
allowed
,该字符串由不同的字符和
字符串数组单词。如果字符串中的所有字符
该字符串出现在允许的字符串中。返回
数组字中的一致字符串
示例:
输入:allowed=“ab”
,words=>[“ad”、“bd”、“aaab”、“baa”、“badab”]
产出:2
说明:字符串“aaab”和“baa”是一致的,因为它们只是
包含字符“a”和“b”
以下是我目前的代码:
注意:allowed包含一个字符串
#include <string.h>
int countConsistentStrings(char * allowed, char ** words, int wordsSize){
int real_count = 0;
for(int i = 0; i < wordsSize; i++)
{
for(int j = 0; j < strlen(allowed); j++)
{
for(int k = 0; k < strlen(words[i]); k++)
{
if(words[i][k] != allowed[j])
{
// stuff goes here?
}
}
}
}
return real_count;
}
#包括
int countConsistentString(允许使用字符*,字符**字,int字大小){
int real_count=0;
for(int i=0;i
我能够逐字符遍历数组中的字符串,并将它们与allowed的字符进行比较。。。但我真的不知道从那以后该怎么办。我曾经尝试过记录资料并进行比较,但后来我遇到了这样一个问题:单词[I]是否小于strlen(允许),或者反之亦然
我知道可能有更简单的方法来解决这个问题(正如我在leetcode的讨论部分所看到的),但我想知道是否有任何方法可以用我已经做过的来解决这个问题
谢谢任何能帮我的人。。。我觉得我已经“差不多”解决了这个问题,但我也被困了很长一段时间,所以我已经准备好接受任何我能得到的解释。这里有一个O(N)解决方案。因此,您不必为每个字符重新扫描allowed
,只需使用查找表即可。很容易,因为字符通常是8位的,256个真/值的数组很容易
int countConsistentStrings(char * allowed, char ** words, int wordsSize) {
// put all the allowed chars into a table of booleans
unsigned int table[256] = {0}; // 256 assumes 8-bit char
int real_count = 0;
const char* ptr = allowed;
while (*ptr)
{
char c = *ptr;
table[(unsigned char)c)] = 1; // table[c] = 1 to indicate "is allowed"
ptr++;
}
// evaluate each word and see if each char in word
// is in the allowed list of chars
for(int i = 0; i < wordsSize; i++)
{
char* word = words[i];
int consistent = 1;
while (*word && consistent)
{
char c = *word;
consistent = table[(unsigned char)c]; // lookup to see if this char is allowed
word++;
}
real_count += consistent;
}
return real_count;
}
int countConsistentString(允许字符*,字符**字,int字大小){
//把所有允许的字符放在一张布尔人的表格里
无符号整数表[256]={0};//256采用8位字符
int real_count=0;
常量字符*ptr=允许;
while(*ptr)
{
字符c=*ptr;
表[(无符号字符)c]=1;//表[c]=1表示“允许”
ptr++;
}
//评估每个单词并查看单词中的每个字符
//在允许的字符列表中
for(int i=0;i
您可以使用strchr
功能帮助检测允许的中是否存在字符。该函数的工作原理如下:
因此,空值测试允许您检查是否在允许的
中找到了有问题的字符。使用此函数,您可以删除一个循环,只需遍历给定单词中的每个字符以查找匹配项。考虑到这一点,我认为假设所有单词都匹配,然后减去不匹配的单词更容易
这里有一些代码说明了我的意思。我创建了一个main函数,该函数调用countConsistentStrings
,使用一些用于验证行为的测试数据
#include <stdio.h>
#include <string.h>
int countConsistentStrings(char *allowed, char **words, int wordsSize)
{
int real_count = wordsSize;
for(int i = 0; i < wordsSize; i++)
{
for(int k = 0; k < strlen(words[i]); k++)
{
// if character is not found, then it's not a consistent string
// so we can just lower the count and exit the loop right now
if(strchr(allowed, words[i][k]) == NULL)
{
real_count--;
break;
}
}
}
return real_count;
}
int main()
{
char *allowed = "abcde";
char *words[] =
{
"abcde",
"fghji",
"d",
"l",
"abcdef"
};
int wordcount = sizeof(words)/sizeof(words[0]);
int count;
printf("Finding consistent strings among %d words\n", wordcount);
count = countConsistentStrings(allowed, words, wordcount);
printf("Consistent string count: %d\n", count);
return 0;
}
#包括
#包括
int countConsistentString(允许使用字符*,字符**字,int字大小)
{
int real_count=wordsSize;
for(int i=0;i
假设允许使用标准C库函数,以下使用“返回由dest
指向的空终止字节字符串的最大初始段(span)长度,该长度仅由由由src
指向的空终止字节字符串中的字符组成”
#包括
int countConsistentString(允许使用字符*,字符**字,int字大小)
{
整数计数=0;
for(int i=0;i
是否允许有一个单词?或者,它可以有多个用空格分隔的单词吗?你能使用strcmp等吗。?旁注:for(int j=0;j
(例如)非常慢(O(n^2)而不是O(n))。更好的做法是:for(int j=0,len=strlen(允许);j
allowed由一个字符串组成。我将修改这个问题,使之清晰明了。。我的循环会是n^2吗?因为每次它都必须迭代strlen(允许)才能得到
#include <stdio.h>
#include <string.h>
int countConsistentStrings(char *allowed, char **words, int wordsSize)
{
int real_count = wordsSize;
for(int i = 0; i < wordsSize; i++)
{
for(int k = 0; k < strlen(words[i]); k++)
{
// if character is not found, then it's not a consistent string
// so we can just lower the count and exit the loop right now
if(strchr(allowed, words[i][k]) == NULL)
{
real_count--;
break;
}
}
}
return real_count;
}
int main()
{
char *allowed = "abcde";
char *words[] =
{
"abcde",
"fghji",
"d",
"l",
"abcdef"
};
int wordcount = sizeof(words)/sizeof(words[0]);
int count;
printf("Finding consistent strings among %d words\n", wordcount);
count = countConsistentStrings(allowed, words, wordcount);
printf("Consistent string count: %d\n", count);
return 0;
}
#include <string.h>
int countConsistentStrings(char *allowed, char **words, int wordsSize)
{
int count = 0;
for(int i = 0; i < wordsSize; i++)
{
char *word = words[i];
if(word[strspn(word, allowed)] == '\0')
count++;
}
return count;
}