私は、次の質問を提起するアルゴリズムの問題セットに取り組んでいます。
「文字列にすべての一意の文字が含まれているかどうかを判断します。配列のみを使用できると仮定します」.
私は実用的な解決策を持っていますが、時間の複雑さに関してより最適化されたものがあるかどうかを確認したいと思います。LINQ を使用したくありません。あなたが提供できる助けに感謝します!
static void Main(string[] args)
{
FindDupes("crocodile");
}
static string FindDupes(string text)
{
if (text.Length == 0 || text.Length > 256)
{
Console.WriteLine("String is either empty or too long");
}
char[] str = new char[text.Length];
char[] output = new char[text.Length];
int strLength = 0;
int outputLength = 0;
foreach (char value in text)
{
bool dupe = false;
for (int i = 0; i < strLength; i++)
{
if (value == str[i])
{
dupe = true;
break;
}
}
if (!dupe)
{
str[strLength] = value;
strLength++;
output[outputLength] = value;
outputLength++;
}
}
return new string(output, 0, outputLength);
}