Friday, January 31, 2014
First-non-repeating-character in string
http://www.geeksforgeeks.org/given-a-string-find-its-first-non-repeating-character/
static char firstNonRepeatedChar(String str)
{
int n = str.length();
if(n == 0) return '#';
if(n == 1) return str.charAt(0);
int hashArr[] = new int[266];
char charArr[] = new char[266];
for(int i = 0 ; i < 266; i++)
{
hashArr[i] = 0;
}
for(int i = 0; i < n; i++)
{
if(hashArr[str.charAt(i)]==0) charArr[i] = str.charAt(i);
hashArr[str.charAt(i)]++;
}
for(int i = 0; i < 266; i++)
{
if(hashArr[charArr[i]] == 1)
{
return charArr[i];
}
}
return '#';
}
static char firstNonRepeatedChar(String str)
{
int n = str.length();
if(n == 0) return '#';
if(n == 1) return str.charAt(0);
int hashArr[] = new int[266];
char charArr[] = new char[266];
for(int i = 0 ; i < 266; i++)
{
hashArr[i] = 0;
}
for(int i = 0; i < n; i++)
{
if(hashArr[str.charAt(i)]==0) charArr[i] = str.charAt(i);
hashArr[str.charAt(i)]++;
}
for(int i = 0; i < 266; i++)
{
if(hashArr[charArr[i]] == 1)
{
return charArr[i];
}
}
return '#';
}
Subscribe to:
Posts (Atom)