Write an algorithm to determine if a string has all unique charcters.
public static boolean isUniqueChars2(String str)
{
boolean [] char_set =new boolean[256];
for(int i=0; i<str.length(); i++)
{
int val=str.charAt(i);
if (char_set[val])
return false;
char_set[val]=true;
}
return true;
}
{
boolean [] char_set =new boolean[256];
for(int i=0; i<str.length(); i++)
{
int val=str.charAt(i);
if (char_set[val])
return false;
char_set[val]=true;
}
return true;
}
2nd Method :
public static boolean isUniqueChars(string str)
{
int checker=0;
for(int i=0;i<str.length();++i)
{
int val=str.charAt(i) -'a';
if((checker & (1<<val))>0)
return false;
checker =(1<<val);
}
return true;
}
Comments