思想
判断字符串strA是否是strB的真子集字符串,思想是避免两个嵌套循环,可以根据char值判断,对比值的大小(我这里假定大字符串是一个递增有序字符串),子串值大于父串值则索引后移,反之迁移;
实现
string strFather = "abcdefs"; string strSon = "bbdsu"; bool result = checkIsSon(strFather, strSon); if (result) System.Console.WriteLine("是真子集"); else System.Console.WriteLine("非真子集"); #region 字符串是否是另一个字符串的真子集 static bool checkIsSon(string strFather, string strSon) { int finalCount = 0; char[] arrFather = strFather.ToArray(); char[] arrSon = strSon.ToArray(); for (int i = 0; i < arrSon.Length; i++) { int son = arrSon[i]; int father = arrFather[i]; if (son == father) { finalCount += 1; } else if (son >= father)//向后找 { for (int j = i; j < arrFather.Length; j++) { if (son == arrFather[j]) { finalCount += 1; break; } if (arrFather[j] > son) { return false; } } } else if (son < father)//向前找 { for (int j = i; j > i; j--) { if (son == arrFather[j]) { finalCount += 1; break; } if (arrFather[j] < son) { return false; } } } } return finalCount == arrSon.Length; } #endregion
输出
非真子集