-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay19.java
More file actions
39 lines (32 loc) · 1.05 KB
/
Day19.java
File metadata and controls
39 lines (32 loc) · 1.05 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
/*Non Repeating Character
Given a string s consisting of lowercase English Letters. return the first non-repeating character in s. If there is no non-repeating character, return '$'.
Examples:
Input: s = "geeksforgeeks"
Output: 'f'
Explanation: In the given string, 'f' is the first character in the string which does not repeat.
Input: s = "racecar"
Output: 'e'
Explanation: In the given string, 'e' is the only character in the string which does not repeat.
Input: s = "aabbccc"
Output: '$'
Explanation: All the characters in the given string are repeating.*/
/*class Solution {
public static char nonRepeatingChar(String s) {
int[] freq = new int[26];
// Count frequency
for (int i = 0; i < s.length(); i++) {
freq[s.charAt(i) - 'a']++;
}
// Find first non-repeating character
for (int i = 0; i < s.length(); i++) {
if (freq[s.charAt(i) - 'a'] == 1) {
return s.charAt(i);
}
}
return '$';
}
}
*/
/*⏱ Complexity
Time: O(n)
Space: O(1) */