when it comes to finding the first non-repeater, we just have to scan the count array, instead of the string
Related: http://massivealgorithms.blogspot.com/2015/07/finding-first-non-repeating-character.html
Read full article from Given a string, find its first non-repeating character | GeeksforGeeks
struct
countIndex {
int
count;
int
index;
};
/* Returns an array of above structure type. The size of
array is NO_OF_CHARS */
struct
countIndex *getCharCountArray(
char
*str)
{
struct
countIndex *count =
(
struct
countIndex *)
calloc
(
sizeof
(countIndex), NO_OF_CHARS);
int
i;
for
(i = 0; *(str+i); i++)
{
(count[*(str+i)].count)++;
// If it's first occurrence, then store the index
if
(count[*(str+i)].count == 1)
count[*(str+i)].index = i;
}
return
count;
}
/* The function returns index of the first non-repeating
character in a string. If all characters are repeating
then reurns INT_MAX */
int
firstNonRepeating(
char
*str)
{
struct
countIndex *count = getCharCountArray(str);
int
result = INT_MAX, i;
for
(i = 0; i < NO_OF_CHARS; i++)
{
// If this character occurs only once and appears
// before the current result, then update the result
if
(count[i].count == 1 && result > count[i].index)
result = count[i].index;
}
free
(count);
// To avoid memory leak
return
result;
}
private class Counter {
private int count;
private int index;
public Counter(int index) {
count = 1;
this.index = index;
}
}
public char find(String s) {
if (s == null)
return ' ';
int len = s.length();
HashMap<Character, Counter> map = new HashMap<Character, Counter>();
for (int i = 0; i < len; i++) {
if (!map.containsKey(s.charAt(i)))
map.put(s.charAt(i), new Counter(i));
else
map.get(s.charAt(i)).count++;
}
Iterator<Entry<Character, Counter>> iter = map.entrySet().iterator();
int index = Integer.MAX_VALUE;
char res = ' ';
while (iter.hasNext()) {
Entry<Character, Counter> entry = iter.next();
if (entry.getValue().count == 1 && entry.getValue().index < index) {
index = entry.getValue().index;
res = entry.getKey();
}
}
return res;
}
We can use string characters as index and build a count array. Following is the algorithm.
1) Scan the string from left to right and construct the count array. 2) Again, scan the string from left to right and check for count of each character, if you find an element who's count is 1, return it.http://yuanhsh.iteye.com/blog/2185878
- int *getCharCountArray(char *str) {
- int *count = (int *)calloc(sizeof(int), NO_OF_CHARS);
- int i;
- for (i = 0; *(str+i); i++)
- count[*(str+i)]++;
- return count;
- }
- /* The function returns index of first non-repeating
- character in a string. If all characters are repeating
- then returns -1 */
- int firstNonRepeating(char *str) {
- int *count = getCharCountArray(str);
- int index = -1, i;
- for (i = 0; *(str+i); i++) {
- if (count[*(str+i)] == 1) {
- index = i;
- break;
- }
- }
- free(count); // To avoid memory leak
- return index;
- }
Related: http://massivealgorithms.blogspot.com/2015/07/finding-first-non-repeating-character.html
Read full article from Given a string, find its first non-repeating character | GeeksforGeeks