This is a very simple function to find if a string is composed of unique characters. I believe that this is a O(n) time complexity as it loops once and has a single if condition. Am I correct? Is there any way it can be optimized further?
public class Program
{
public static void Main(string[] args)
{
DuplicateChars(args[0].ToLower());
}
public static bool DuplicateChars(string str)
{
bool[] charfound = new bool[26];
int index = 0;
for(int i = 0; i <= str.Length ; ++i)
{
index = (byte)(str[i]);
//subtract by ascii value of small a 97.
index -= 97;
if(charfound[index])
return true;
else
charfound[index] = true;
}
return false;
}
}