public class FirstNonRepeating {
static final int NO_OF_CHARS = 256;
static char count[] = new char[NO_OF_CHARS];
static void getCharCountArray(String str)
{
for (int i = 0; i < str.length(); i++)
count[str.charAt(i)]++;
}
static int firstNonRepeating(String str)
{
getCharCountArray(str);
int index = -1, i;
for (i = 0; i < str.length(); i++) {
if (count[str.charAt(i)] == 1) {
index = i;
break;
}
}
return index;
}
public static void main(String[] args) {
String str = "algorithm";
int index = firstNonRepeating(str);
if(index == -1){
System.out.println("First non-repeating character does not exists");
}else {
System.out.println("First non-repeating character is " + str.charAt(index));
}
}
}
TechInsiderStory is place where you can find basic knowledge of various technology and framework. TechInsiderStory is provide free knowledge on various technology and framework.
First Non Repeating Character
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment