Finding common characters in two strings

前端 未结 13 2503
终归单人心
终归单人心 2020-12-15 14:09

I am coding for the problem in which we got to count the number of common characters in two strings. Main part of the count goes like this

for(i=0; i < st         


        
相关标签:
13条回答
  • int count(string a, string b) 
    {
    
       int i,c[26]={0},c1[26]={};
    
       for(i=0;i<a.length();i++)
       {
            if(97<=a[i]&&a[i]<=123)
            c[a[i]-97]++;
       }
       for(i=0;i<b.length();i++)
       {
           if(97<=b[i]&&b[i]<=123)
            c1[b[i]-97]++;
        } 
        int s=0;
        for(i=0;i<26;i++)
        {
            s=s+abs(c[i]+c1[i]-(c[i]-c1[i]));
    
        }   
    
        return (s);  
    }
    

    This is much easier and better solution

    0 讨论(0)
  • 2020-12-15 14:26

    Python Code:

        >>>s1='abbc'    
        >>>s2='abde'
        >>>p=list(set(s1).intersection(set(s2)))
        >>print(p)
        ['a','b']
    

    Hope this helps you, Happy Coding!

    0 讨论(0)
  • 2020-12-15 14:27

    Following code traverses each sting only once. So the complexity is O(n). One of the assumptions is that the upper and lower cases are considered same.

     #include<stdio.h>
    
    int main() {
        char a[] = "Hello world";
        char b[] = "woowrd";
        int x[26] = {0};
        int i;
        int index;
    
        for (i = 0; a[i] != '\0'; i++) {
            index = a[i] - 'a';
            if (index > 26) {
                //capital char
                index = a[i] - 'A';
            }
            x[index]++;
        }
    
        for (i = 0; b[i] != '\0'; i++) {
            index = b[i] - 'a';
            if (index > 26) {
                //capital char
                index = b[i] - 'A';
            }
    
            if (x[index] > 0)
                x[index] = -1;
        }
    
        printf("Common characters in '%s' and '%s' are ", a, b);
        for (i = 0; i < 26; i++) {
            if (x[i] < 0)
                printf("%c", 'a'+i);
        }
        printf("\n");
    }
    
    0 讨论(0)
  • 2020-12-15 14:28
    for (std::vector<char>::iterator i = s1.begin(); i != s1.end(); ++i)
    {
        if (std::find(s2.begin(), s2.end(), *i) != s2.end())
       {
        dest.push_back(*i);
       }
    }
    

    taken from here

    0 讨论(0)
  • 2020-12-15 14:34

    It can be done in O(n) time with constant space.

    The pseudo code goes like this :

    int map1[26], map2[26];
    int common_chars = 0;
    
    for c1 in string1:
        map1[c1]++;
    
    for c2 in string2:
        map2[c2]++;
    
    for i in 1 to 26:
        common_chars += min(map1[i], map2[i]);
    
    0 讨论(0)
  • 2020-12-15 14:37

    Your current code is O(n^3) because of the O(n) strlens and produces incorrect results, for example on "aa", "aa" (which your code will return 4).

    This code counts letters in common (each letter being counted at most once) in O(n).

    int common(const char *a, const char *b) {
        int table[256] = {0};
        int result = 0;
        for (; *a; a++)table[*a]++;
        for (; *b; b++)result += (table[*b]-- > 0);
        return result;
    }
    

    Depending on how you define "letters in common", you may have different logic. Here's some testcases for the definition I'm using (which is size of the multiset intersection).

    int main(int argc, char *argv[]) {
        struct { const char *a, *b; int want; } cases[] = {
            {"a", "a", 1},
            {"a", "b", 0},
            {"a", "aa", 1},
            {"aa", "a", 1},
            {"ccc", "cccc", 3},
            {"aaa", "aaa", 3},
            {"abc", "cba", 3},
            {"aasa", "asad", 3},
        };
        int fail = 0;
        for (int i = 0; i < sizeof(cases) / sizeof(*cases); i++) {
            int got = common(cases[i].a, cases[i].b);
            if (got != cases[i].want) {
                fail = 1;
                printf("common(%s, %s) = %d, want %d\n",
                       cases[i].a, cases[i].b, got, cases[i].want);
            }
        }
        return fail;
    }
    
    0 讨论(0)
提交回复
热议问题