同じ文字で異なる単語を識別できるアナグラム ファインダー プログラムを作成しています。例えば:
input: ['cars', 'for', 'potatoes', 'racs', 'four', 'scar', 'creams', 'scream']
output: [ ["cars", "racs", "scar"], ["four"], ["for"],
["potatoes"], ["creams","scream"] ]
これは私が現在持っているコードです。
public List<List<string>> AnagramsFinder(List<string> words)
{
List<List<string>> returnList = new List<List<string>>();
for (int i = 0; i < words.Count; i++)
{
char[] inputCharArray = words[i].ToCharArray();
Array.Sort(inputCharArray);
string inputString = new string(inputCharArray);
if (ReturnList.Contains(InputString))
{
ReturnList[InputString].Add(words[i]);
}
else
{
ReturnList.Add(InputString, new List<string>());
ReturnList[InputString].Add(words[i]);
}
}
return returnList;
}
if-else 文は表現したいものですが、.NET WCF 形式では有効ではありません。同じ目的を達成するためにif-elseステートメントを変更するのを手伝ってくれる人はいますか? どうもありがとう!!