2

我正在研究一个提出以下问题的算法问题集:

“确定一个字符串是否具有所有唯一字符。假设您只能使用数组”。

我有一个可行的解决方案,但我想看看在时间复杂度方面是否有更好的优化。我不想使用 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);
}
4

7 回答 7

10

如果您只关心时间复杂度,您可以将字符映射到 int 值,然后拥有一个 bool 值数组,这些值会记住您之前是否看过特定的字符值。

像... [未测试]

bool[] array = new bool[256]; // or larger for Unicode

foreach (char value in text)
  if (array[(int)value])
    return false;
  else
    array[(int)value] = true;

return true; 
于 2012-10-01T04:29:08.050 回答
3

试试这个,

    string RemoveDuplicateChars(string key)
    {
        string table = string.Empty;
        string result = string.Empty;
        foreach (char value in key)
        {
            if (table.IndexOf(value) == -1)
            {
                table += value;
                result += value;
            }
        }
        return result;
    }

用法

Console.WriteLine(RemoveDuplicateChars("hello"));
Console.WriteLine(RemoveDuplicateChars("helo"));
Console.WriteLine(RemoveDuplicateChars("Crocodile"));

输出

helo
helo
Crocdile
于 2012-10-01T04:10:33.123 回答
1
public boolean ifUnique(String toCheck){
    String str="";
    for(int i=0;i<toCheck.length();i++)
    {
         if(str.contains(""+toCheck.charAt(i)))
             return false;
         str+=toCheck.charAt(i);
    }
    return true;
}

编辑:

您也可以考虑省略 toCheck 是空字符串的边界情况。

于 2012-10-01T05:04:58.677 回答
0

以下代码有效:

 static void Main(string[] args)
    {
        isUniqueChart("text");
        Console.ReadKey();

    }

    static Boolean isUniqueChart(string text)
    {
        if (text.Length == 0 || text.Length > 256)
        {
            Console.WriteLine(" The text is empty or too larg");
            return false;
        }
        Boolean[] char_set = new Boolean[256];
        for (int i = 0; i < text.Length; i++)
        {
            int val = text[i];//already found this char in the string
            if (char_set[val])
            {
                Console.WriteLine(" The text is not unique");
                return false;
            }
            char_set[val] = true;
        }
        Console.WriteLine(" The text is unique");
        return true;
    }
于 2013-04-09T20:01:30.690 回答
0

如果字符串只有小写字母 (az) 或只有大写字母 (AZ),您可以使用非常优化的 O(1) 解决方案。也可以使用 O(1) 空间。

C++代码:

bool checkUnique(string s){
    if(s.size() >26)
        return false;
    int unique=0;
    for (int i = 0; i < s.size(); ++i) {
        int j= s[i]-'a';
        if(unique & (1<<j)>0)
            return false;
       unique=unique|(1<<j);
   }
   return true;
}
于 2015-11-24T21:11:47.340 回答
0
于 2019-09-22T08:57:05.200 回答
-1
public static bool CheckUnique(string str)
{
    int accumulator = 0;
    foreach (int asciiCode in str)
    {
        int shiftedBit = 1 << (asciiCode - ' ');
        if ((accumulator & shiftedBit) > 0)
            return false;
        accumulator |= shiftedBit;
    }
    return true;
}
于 2019-09-22T08:00:13.457 回答