字符串重复的最简单的解决方案

时间:2013-09-06 02:28:43

标签: c# string duplicates complexity-theory

找到重复字符的最简单算法是什么?

string a = "school";
string b = "ofrock";

输出应该是o,c(不是ooc)。你能找到O(n)线性复杂度吗?我不能

string a = "School";
string b = "ofRock";
string c = a + b;
char[] cc = c.ToCharArray();

Dictionary<char, int> d = new Dictionary<char, int>();
Dictionary<char, int> l = new Dictionary<char, int>();

foreach (char ccc in cc)
{
    try
    {
        d.Add(ccc, 1);
    }
    catch
    {
        try
        {
            l.Add(ccc, 1);
        }
        catch
        {
        }
    }
}

1 个答案:

答案 0 :(得分:3)

简单的方法,您可以使用Intersect

var result = a.Intersect(b);