面试题:
求字符s1 = "lookyou";和s2 = "likeyou";最长字符交集。输出:“you”
用一个函数实现;
C#实现:
public string Find()
{
string s1 = "lookyou";
string s2 = "likeyou";
List<string> lst2 = new List<string>();
int k2 = s2.Length;
for (int j = 0; j < k2; j++)
{
string tempS = s2.Substring(j);
for (int i = 0; i < tempS.Length; i++)
{
lst2.Add(tempS.Substring(0, i + 1));
}
} string tempS1 = "";
for (int i = 0; i < lst2.Count; i++)
{
string temp2 = lst2[i];
if (s1.IndexOf(temp2) > -1)
{
if (temp2.Length > tempS1.Length)
{
tempS1 = temp2;
}
}
}
return tempS1;
}