#2 Daily Problems -FirstNotRepeatingCharacter
1 min readJul 19, 2020
As I already mention in previous post, I have planned to do solve one problem per day.
Problem:
- For
s = "abacabad"
, the output should befirstNotRepeatingCharacter(s) = 'c'
. - There are
2
non-repeating characters in the string:'c'
and'd'
. Returnc
since it appears in the string first. - For
s = "abacabaabacaba"
, the output should befirstNotRepeatingCharacter(s) = '_'
. - There are no characters in this string that do not repeat.
Input/Output
- [input] string s
- A string that contains only lowercase English letters.
- Guaranteed constraints:
1 ≤ s.length ≤ 105
. - The first non-repeating character in
s
, or'_'
if there are no characters that do not repeat.
My Solution:
char firstNotRepeatingCharacter(String s) {HashSet<Character> hS=new HashSet<>();String tS=s;for (int i=0;i<s.length();i++){if(!hS.contains(s.charAt(i)))hS.add(s.charAt(i));elsetS= tS.replace(""+s.charAt(i),"");}if (tS.length()>0)return tS.charAt(0);elsereturn '_';}
Again hi guys, please just see my solution and help me to improve this if you have any suggestion for me