Sunday, March 15, 2020

Find out if String has all Unique Characters

In the technical interviews ,you may come across this question many times ,determine whether a string has all unique characters or not . Before moving to the solution , here in this question   we have taken one assumption that is  all the characters in the string are ASCII characters.
For those who are not familiar with ASCII characters.
ASCII abbreviation is known as American Standard Code for  Information Interchange.
In simple words it is just the number representation of  characters.
Find out if String has all Unique Characters

So , First understand the question by writing examples :
Input : Alive is awesome
Output : false
Input : Live present moment
Output : false
Input : Alive swum
Output: true
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
public class uniquechar {
   
    public static void main (String args[])
    {
        boolean result=false;
        String inputstring="Alve i@wsom";
        System.out.println(inputstring);
        HashSet < Character> uniquecharset= new HashSet();
        for(int i=0;i < inputstring.length();i++)
        {
            result=uniquecharset.add(inputstring.charAt(i));
            if (result == false)
            break;
        }
    System.out.println(result); }
}

No comments:

Post a Comment