[算法]寻找字母重复组合的单词

    技术2022-05-11  26

    //有字符串如"ABCD,BDAC,DRF,FRD,CADB,CAADB,XB",要将其中由相同字符组成的词归在一起,如果没有和其由相同字符组成的词则去掉,如上的结果为: //1ABCD,BDAC,CADB //2: DRF,FRD //另外,CAADBXB去掉   string vText= @"ABCD,BDAC,DRF,FRD,CADB,CAADB,XB"; List<string> vResults = new List<string>(); string[] vWords= vText.Split(",".ToCharArray(),      StringSplitOptions.RemoveEmptyEntries); foreach (string vWord in vWords) {       char[] vChars = vWord.ToUpper().ToCharArray();     Array.Sort(vChars);     string T = new string(vChars);     bool vExists = false;     for (int i = 0; i < vResults.Count; i++)         if (T + "=" == vResults[i].Substring(0, T.Length + 1))         {             vResults[i] += "," + vWord;             vExists = true;             break;         }     if (!vExists) vResults.Add(T + "=" + vWord); } int vGroup = 0; foreach (string vResult in vResults) {     if (vResult.IndexOf(',') > 0)     {         vGroup++;         textBox1.AppendText(string.Format("{0}{1}/r/n",             vGroup, vResult.Substring(vResult.IndexOf('=') + 1)));     } }    

    最新回复(0)