-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNonRepeating1.java
More file actions
105 lines (93 loc) · 3.79 KB
/
NonRepeating1.java
File metadata and controls
105 lines (93 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
/**
* Java Program to find first duplicate, non-repeated character in a String.
* It demonstrate three simple example to do this programming problem.
*
* @author Javarevisited
*/
public class Programming {
/*
* Using LinkedHashMap to find first non repeated character of String
* Algorithm :
* Step 1: get character array and loop through it to build a
* hash table with char and their count.
* Step 2: loop through LinkedHashMap to find an entry with
* value 1, that's your first non-repeated character,
* as LinkedHashMap maintains insertion order.
*/
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");
}
/*
* Finds first non repeated character in a String in just one pass.
* It uses two storage to cut down one iteration, standard space vs time
* trade-off.Since we store repeated and non-repeated character separately,
* at the end of iteration, first element from List is our first non
* repeated character from String.
*/
public static char firstNonRepeatingChar(String word) {
Set<Character> repeating = new HashSet<>();
List<Character> nonRepeating = new ArrayList<>();
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);
}
/*
* Using HashMap to find first non-repeated character from String in Java.
* Algorithm :
* Step 1 : Scan String and store count of each character in HashMap
* Step 2 : traverse String and get count for each character from Map.
* Since we are going through String from first to last character,
* when count for any character is 1, we break, it's the first
* non repeated character. Here order is achieved by going
* through String again.
*/
public static char firstNonRepeatedCharacter(String word) {
HashMap<Character,Integer> scoreboard = new HashMap<>();
// build table [char -> count]
for (int i = 0; i < word.length(); i++) {
char c = word.charAt(i);
if (scoreboard.containsKey(c)) {
scoreboard.put(c, scoreboard.get(c) + 1);
} else {
scoreboard.put(c, 1);
}
}
// since HashMap doesn't maintain order, going through string again
for (int i = 0; i < word.length(); i++) {
char c = word.charAt(i);
if (scoreboard.get(c) == 1) {
return c;
}
}
throw new RuntimeException("Undefined behaviour");
}
}
Read more: https://javarevisited.blogspot.com/2014/03/3-ways-to-find-first-non-repeated-character-String-programming-problem.html#ixzz6DCR2JiKb