找到重复字符的最简单算法是什么?
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
{
}
}
}
答案 0 :(得分:3)
简单的方法,您可以使用Intersect
:
var result = a.Intersect(b);