Wednesday, August 21, 2019

Given a string, find its first non-repeating character

Given a string, find the first non-repeating character in it.




Solution:

  • Using Array-
    1.  Create an arrays (count[]) of size 256 (Total ASCII codes).
    2. Read the input string character by character and increament values of count[] at index corresponding to the ASCII value of the current character.
    3. To find the first non-repeating character, again read through the input, character by character and check if it's count is equal to 1 from the count[] array.
    4. If count[] is equal to 1, print character and exit.
    5. Else print no "non repeating character found".
  •  Using HashMap-
    1. Create a HashMap count<Character, Integer>.  
    2. Read the input string character by character,
      • If character already present, increament it's count
      • Else insert the character with count as 1.
    3. To find the first non-repeating character, again read through the input, character by character and check if it's count in the HashMap is equal to 1,
      • If equal to 1, print character and exit.
      • Else print no "non repeating character found".

 

Java code-

Using Array-

public class FirstNonRepeatingCharacterUsingArray
{
    public static void main(String[] args)
    {
        int count[] = new int[256];
        String s = "MuscleJavaMuscle";
        for(int i=0; i<s.length(); i++)
        {
            count[ s.charAt(i) ]++ ;
        }
        System.out.println("First non-repeating character- ");
        for(int i=0; i<s.length(); i++)
        {
            if(count[ s.charAt(i) ] == 1)
            {
                System.out.println(s.charAt(i));
                System.exit(0);     //exit program if required
                                    //output is found
            }
        }
        /*this part of code will run if
        the first non repeating character is not found*/
        System.out.println("Not found");       
    }
}

Using HashMap-

public class FirstNonRepeatingCharUsingHashMap
{
    public static void main(String[] args)
    {
        HashMap<Character, Integer> count = new HashMap<>();
        String s = "MuscleJavaMuscle";
        for(int i=0; i<s.length(); i++)
        {
            if(!count.containsKey(s.charAt(i))) //if not present in
                                                //HashMap
                count.put(s.charAt(i), 1);      //put the char and
                                                //its count as '1'
            else        //if present, increase it count
                count.replace(s.charAt(i),count.get(s.charAt(i))+1);
        }
        //to print
        for(int i=0; i<s.length(); i++)
        {
            if(count.get(s.charAt(i)) == 1)
            {
                System.out.print("First non-repeating "
                        + "character- " + s.charAt(i));
                System.exit(0);
            }
        }
        /*this part of code will run if
        the first non repeating character is not found*/
        System.out.println("Not found");
    }
}

Output-

First non-repeating character- J 

 


No comments:

Post a Comment