计算C中一致字符串的数量 [英] Count the Number of Consistent Strings in C

查看:60
本文介绍了计算C中一致字符串的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这是leetcode的问题,我遇到了一些问题..我已经看到解决该问题的代码发布到leetcode的讨论部分,但是我想知道是否有人可以帮助我解决问题我已经编写了一些代码.这是问题所在:

So this is a problem off leetcode and I'm having some issues with it.. I've seen code that solves this problem posted to the discussion's portion of leetcode, but I was wondering if anyone could help me solve it with some of the code I've already written.. Here's what the problem is asking:

系统会为您提供一个字符串 allowed (由不同的字符组成,字符串 words 的数组.如果所有字符都在字符串中,则字符串是一致的该字符串出现在字符串 allowed 中.返回数量数组单词中的字符串一致.

You are given a string allowed consisting of distinct characters and an array of strings words. A string is consistent if all characters in the string appear in the string allowed. Return the number of consistent strings in the array words.

示例:
输入: allowed ="ab" words =>["ad","bd","aaab","baa","badab"]

输出:2

说明:字符串"aaab"和"baa"是一致的,因为它们只是包含字符"a"和"b".

Explanation: Strings "aaab" and "baa" are consistent since they only contain characters 'a' and 'b'.

到目前为止,这是我的代码:

Here's my code so far:

注意:允许包含一个字符串

Note: allowed contains a single string

#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;
}

我能够逐个char遍历char数组中的字符串,并将它们与允许的chars进行比较...但是我真的不知道从那里去哪里.我尝试过统计所有内容并使用比较,但随后遇到了单词[i]小于strlen(allowed)还是反之亦然的问题.

I'm able to iterate through the strings in the array char by char and compare them to allowed's chars... But I really don't know where to go from there. I've tried keeping tallies of stuff and using comparisons but then I run into issues of whether words[i] is less than strlen(allowed) or vice-versa..

我知道解决这个问题的方法可能更简单(正如我在leetcode的讨论部分所看到的那样),但是我想知道是否有办法使用已经完成的方法解决它?

I know there's probably easier ways to do this problem (as I've seen posted on the discussion portion of leetcode) but I'm wondering if there's anyway to solve it using what I've already done?

谢谢任何可以帮助我解决这个问题的人...我觉得我差不多"那里有这个问题,但是我也已经坚持了很长时间,所以我已经准备好可以得到任何解释了.

Thank you anyone who can help me out with this... I feel like I'm "almost" there with this problem, but I've also been stuck on it quite a while so I'm ready for any explanations I can get.

推荐答案

您可以使用 strchr 函数来帮助检测是否在 allowed 中存在字符.该函数的工作方式如下:

You can use strchr function to help detect if a character exists in allowed. The function works like this:

   char *strchr(const char *s, int c);
   The strchr() function returns a pointer to the first occurrence
   of the character c in the string s.
   The strchr() and strrchr() functions return a pointer to the
   matched character or NULL if the character is not found.

因此,对NULL进行测试可让您检查是否在 allowed 中找到了该字符.使用此功能,您可以删除一个循环,并仅循环访问给定单词中的每个字符以查找匹配项.考虑到这一点,我认为假设所有单词 do 匹配然后减去不匹配的单词会更容易.

So testing for NULL allows you to check if the character in question is found in allowed. With this function, you can remove one of your loops and just iterate through each character in a given word looking for matches. With this in mind, I think it's easier to assume all words do match then subtract out the ones that don't match.

这里有一些代码可以说明我的意思.我创建了一个主要函数,该函数使用用于验证行为的一些测试数据调用 countConsistentStrings .

Here is some code that illustrates what I mean. I created a main function that calls countConsistentStrings with some test data I used to verify the behavior.

#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;
}

这篇关于计算C中一致字符串的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆