http://hehejun.blogspot.com/2015/01/algorithmfirst-non-repeating-character.html
一个很简单的方法就是扫两遍string,第一遍记录每一个字符的数量,更新map,第二次从左往右找到第一个出现次数是1的即可。但是当string很长的时候two-pass的方法效率不是很好,如果要求one-pass的话,我们第二遍扫map即可,因为当string很长是,相对于string,map的长度是很小的,所以我们在存map的时候,除了存count还需要把第一次出现的index也存下来
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;
}
Problem solving with programming: Finding the first non repeating character in a string
Given a string as input, how do we find the first non-repeating character?
http://www.geeksforgeeks.org/given-a-string-find-its-first-non-repeating-character/
The first part of the algorithm runs through the string to construct the count array (in O(n) time). This is reasonable. But the second part about running through the string again just to find the first non-repeater is not good in practice. In real situations, your string is expected to be much larger than your alphabet. Take DNA sequences for example: they could be millions of letters long with an alphabet of just 4 letters. What happens if the non-repeater is at the end of the string? Then we would have to scan for a long time (again).
We can augment the count array by storing not just counts but also the index of the first time you encountered the character e.g. (3, 26) for ‘a’ meaning that ‘a’ got counted 3 times and the first time it was seen is at position 26. So when it comes to finding the first non-repeater, we just have to scan the count array, instead of the string.
http://javarevisited.blogspot.com/2014/03/3-ways-to-find-first-non-repeated-character-String-programming-problem.html
一个很简单的方法就是扫两遍string,第一遍记录每一个字符的数量,更新map,第二次从左往右找到第一个出现次数是1的即可。但是当string很长的时候two-pass的方法效率不是很好,如果要求one-pass的话,我们第二遍扫map即可,因为当string很长是,相对于string,map的长度是很小的,所以我们在存map的时候,除了存count还需要把第一次出现的index也存下来
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;
}
Problem solving with programming: Finding the first non repeating character in a string
Given a string as input, how do we find the first non-repeating character?
public static int getFirstNonRepeating(String input) { int[] charCount = new int[256]; int i; for( i = 0; i < input.length(); i++) {
//we use the character itself as an index.
charCount[ input.charAt(i) ]++; } for( i = 0; i < input.length(); i++) { if( charCount[ input.charAt(i) ] == 1) return i; } return -1; }
http://www.geeksforgeeks.org/given-a-string-find-its-first-non-repeating-character/
The first part of the algorithm runs through the string to construct the count array (in O(n) time). This is reasonable. But the second part about running through the string again just to find the first non-repeater is not good in practice. In real situations, your string is expected to be much larger than your alphabet. Take DNA sequences for example: they could be millions of letters long with an alphabet of just 4 letters. What happens if the non-repeater is at the end of the string? Then we would have to scan for a long time (again).
We can augment the count array by storing not just counts but also the index of the first time you encountered the character e.g. (3, 26) for ‘a’ meaning that ‘a’ got counted 3 times and the first time it was seen is at position 26. So when it comes to finding the first non-repeater, we just have to scan the count array, instead of the string.
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;
}
http://javarevisited.blogspot.com/2014/03/3-ways-to-find-first-non-repeated-character-String-programming-problem.html
Read full article from Problem solving with programming: Finding the first non repeating character in a string public static char getFirstNonRepeatedChar(String str) { Map<Character,Integer>counts = new LinkedHashMap<>(str.length()); for (char c : str.toCharArray()) { counts.put(c, counts.containsKey(c) ? counts.get(c) + 1 : 1); } for (Entry<Character,Integer> entry : counts.entrySet()) { if (entry.getValue() == 1) { return entry.getKey(); } } throw new RuntimeException("didn't find any non repeated Character"); } public static char firstNonRepeatingChar(String word) { Set<Character> repeating = new HashSet<>(); List<Character> nonRepeating = new ArrayList<>(); // use linkedhashset for (int i = 0; i < word.length(); i++) { char letter = word.charAt(i); if (repeating.contains(letter)) { continue; } if (nonRepeating.contains(letter)) { nonRepeating.remove((Character) letter); repeating.add(letter); } else { nonRepeating.add(letter); } } return nonRepeating.get(0); }