【在 a***n 的大作中提到】 : 谢谢,可是我的问题不完全一样, : i want to check if string A contains any permutation of string B, for : example, string A : absditg, string B: ba, then it is yes
【在 a***n 的大作中提到】 : 谢谢,可是我的问题不完全一样, : i want to check if string A contains any permutation of string B, for : example, string A : absditg, string B: ba, then it is yes
我的方法:通过一个顺序无关的校验码,找到“疑似”字串,在对“疑似”进行甄别。 public static int GetCode(char c) { return c * (c ^ 23405) % 74259; } public static bool IsPermut(string a, string b) { List chars = new List(a.ToArray()); foreach (char ch in b) if (chars.Contains(ch)) chars.Remove(ch); else return false; return true; } public static void FindPermut(string a, string b) { if (a.Length >= b.Length) { int codeSumB = 0; for (int i = 0; i < b.Length; i++) codeSumB += GetCode(b[i]); int codeSumA = 0; for (int i = 0; i < a.Length; i++) { codeSumA += GetCode(a[i]); if (i >= b.Length - 1) { if (codeSumA == codeSumB) { string candidate = a.Substring(i - b.Length + 1, b.Length); if (IsPermut(candidate, b)) Console.WriteLine("Start:{0}\t{1}", i - b.Length + 1, candidate); }