#2 Daily Problems -FirstNotRepeatingCharacter

Arun Pandian M
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 be
    firstNotRepeatingCharacter(s) = 'c'.
  • There are 2 non-repeating characters in the string: 'c' and 'd'. Return c since it appears in the string first.
  • For s = "abacabaabacaba", the output should be
    firstNotRepeatingCharacter(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

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Arun Pandian M
Arun Pandian M

Written by Arun Pandian M

Senior Android developer at FundsIndia, A time investor to learn new things about Programming. Currently in a relationship with Green Bug(Android).

No responses yet