-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathFirstNonRepeatingCharacter.java
More file actions
41 lines (37 loc) · 1.16 KB
/
FirstNonRepeatingCharacter.java
File metadata and controls
41 lines (37 loc) · 1.16 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
/**
Given a string A denoting a stream of lowercase alphabets. You have to make new string B.
B is formed such that we have to find first non-repeating character each time a character is inserted to the stream and append it at the end to B. If no non-repeating character is found then append '#' at the end of B
INPUT : "abadbc"
OUTPUT: "aabbdd"
**/
public class Solution {
public String solve(String A) {
int[] frequency = new int[26];
Queue<Character> queue = new LinkedList<Character>();
int i = 0;
StringBuffer answer = new StringBuffer("");
while(i < A.length())
{
queue.add(A.charAt(i));
frequency[A.charAt(i) - 97]++;
while(!queue.isEmpty())
{
if(frequency[queue.peek() - 97] > 1)
{
queue.poll();
}
else
{
answer.append(queue.peek());
break;
}
}
if(queue.isEmpty())
{
answer.append('#');
}
i++;
}
return answer.toString();
}
}