C#查找字符串所有排列組合的方法
更新時間:2015年04月27日 11:29:30 作者:lele
這篇文章主要介紹了C#查找字符串所有排列組合的方法,涉及C#字符串操作的相關(guān)技巧,非常具有實用價值,需要的朋友可以參考下
本文實例講述了C#查找字符串所有排列組合的方法。分享給大家供大家參考。具體實現(xiàn)方法如下:
// 1. remove first char
// 2. find permutations of the rest of chars
// 3. Attach the first char to each of those permutations.
// 3.1 for each permutation, move firstChar in all indexes
// to produce even more permutations.
// 4. Return list of possible permutations.
public string[] FindPermutations(string word)
{
if (word.Length == 2)
{
char[] _c = word.ToCharArray();
string s = new string(new char[] { _c[1], _c[0] });
return new string[]
{
word,
s
};
}
List<string> _result = new List<string>();
string[] _subsetPermutations = FindPermutations(word.Substring(1));
char _firstChar = word[0];
foreach (string s in _subsetPermutations)
{
string _temp = _firstChar.ToString() + s;
_result.Add(_temp);
char[] _chars = _temp.ToCharArray();
for (int i = 0; i < _temp.Length - 1; i++)
{
char t = _chars[i];
_chars[i] = _chars[i + 1];
_chars[i + 1] = t;
string s2 = new string(_chars);
_result.Add(s2);
}
}
return _result.ToArray();
}
希望本文所述對大家的C#程序設計有所幫助。
相關(guān)文章
用序列化實現(xiàn)List<T> 實例的深復制(推薦)
下面小編就為大家?guī)硪黄眯蛄谢瘜崿F(xiàn)List<T> 實例的深復制(推薦)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02
c# Winform同一數(shù)據(jù)源多個控件保持同步
通過對控件屬性設置數(shù)據(jù)源綁定,利用Windows數(shù)據(jù)更改通知這一特性,只要訂閱(設定綁定)的控件都能接收到數(shù)據(jù)的變化通知。 通過DataBindings方法實現(xiàn)雙向數(shù)據(jù)綁定2021-06-06

