Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write a function that compares two strings and returns a third string containing only the letters that appear in both

I got this homework. And have solved it in following way. I need your comments whether it is a good approach or I need to use any other data sturcture to solve it in better way.

    public string ReturnCommon(string firstString, string scndString)
    {
        StringBuilder newStb = new StringBuilder();
        if (firstString != null && scndString != null)
        {
            foreach (char ichar in firstString)
            {
                if (!newStb.ToString().Contains(ichar) && scndString.Contains(ichar))
                    newStb.Append(ichar);
            }
        }
        return newStb.ToString();
    }
like image 250
Pritam Karmakar Avatar asked Jan 15 '10 10:01

Pritam Karmakar


People also ask

How do you compare two strings have the same characters?

To check if two strings have the same characters:Use the sorted() function to sort the two strings. Use the equality operator to compare the results. If the comparison evaluates to True , the two strings have the same characters.

How do you compare letters in a string in python?

String Comparison using == in PythonThe == function compares the values of two strings and returns if they are equal or not. If the strings are equal, it returns True, otherwise it returns False.

How do you know if two strings s1 and s2 have the same value?

3) By Using compareTo() method The String class compareTo() method compares values lexicographically and returns an integer value that describes if first string is less than, equal to or greater than second string. Suppose s1 and s2 are two String objects. If: s1 == s2 : The method returns 0.


2 Answers

For an alternative solution, you can view the strings as enumerables and use Intersect() like this:

    public static string Common(string first, string second)
    {
        return new string((first.Intersect(second)).ToArray());
    }
like image 89
Brian Rasmussen Avatar answered Oct 22 '22 20:10

Brian Rasmussen


That's fine for a first approach, but you can make a few improvements, and there's a small error.

  • If b contains a character in a that's already in c, you'll repeat it.
  • To avoid repeats, you might consider using a Set to store the characters, since a Set won't have repeats.
  • Assembling strings with += concatenation is usually inefficient; consider using a StringBuilder or an analogous string-assembly class.
  • Your variable names aren't very descriptive.
  • If a or b are empty, you don't have to do any work at all! Just return an empty string.

You can think about some more sophisticated improvements, too, by imagining how your algorithm scales if you started to use huge strings. For example, one approach might be that if one string is much longer than the other, you can sort the longer one and remove duplicates. Then you can do a binary search on the characters of the shorter string very quickly.

like image 22
John Feminella Avatar answered Oct 22 '22 20:10

John Feminella