How to Read Unique Character in a String Java

In the technical interviews ,you may come beyond this question many times ,determine whether a cord has all unique characters or not . Earlier moving to the solution , hither 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 every bit American Standard Lawmaking for  Information Interchange.
In unproblematic words it is just the number representation of  characters.

So , First understand the question by writing examples :

Input : Alive is crawly
Output : false

Input : Live present moment
Output : faux

Input : Alive swum
Output: true

PseudoCode for Method 1 :

1. Create a HashSet object.
2. Scan the whole string, and add together each character one by ane to the HashSet object
3. If the add object  returns true then go on
else render false

Method 1

            import            coffee.util.ArrayList;            import            java.util.Collections;            import            java.util.HashSet;            public            form            uniquechar            {            public            static            void            primary            (String args[])     {            boolean            event=false;         Cord inputstring="Alve i@wsom";         System.out.println(inputstring);         HashSet < Character> uniquecharset=            new            HashSet();              for(int              i=0;i < inputstring.length();i++)         {             upshot=uniquecharset.add together(inputstring.charAt(i));              if              (issue ==              false)              break;         }     Organization.out.println(result); } }                      

PseudoCode for Method 2 :

one. Scan the input cord , take each character one by i and set count flag to 0.
2. For each character in the inputstring ,Rescan the inputstring and compare the character with each character appear in the inputstring
3. If equal then increment the count by 1
else continue the loop
4.  If count flag value is greater than ane then render false
else render true

Method ii

            import            java.util.ArrayList;            import            java.util.Collections;            import            java.util.HashSet;            public            class            UniqueChar2 {            public            static            void            primary            (String args[])     {            boolean            event=fake;         String inputstring="Alive is awesome";         System.out.println("String method ii answer "+ method2(inputstring));     }            public            static            boolean            method2(String input)     {            for(int            i=0; i < input.length();i++)         {            char            charcterofinputstring=input.charAt(i);            int            count=0;            for(int            j=              i            ; j < input.length();j++)             {            if            (charcterofinputstring==input.charAt(j))                 count++;             }            if(count >            1)            return            false;         }            return            true;     } }
          
        

PseudoCode for Method 3:

i. indexOf() returns the index of first occurence of the character or else render -1. And then , here nosotros are creating an arraylist object.
ii. Scan the inputstring and add the index of each character to the arraylist object.
iii. Sort the arraylist object.

unique characters in string java example

4. Compare the values of  each adjacent positions of arraylist object
if equal so return false
else go along scanning the arraylist

v.  return true

Method 3

            import            java.util.ArrayList;            import            coffee.util.Collections;            import            coffee.util.HashSet;            public            grade            UniqueChar3            {            public            static            void            main            (String args[])     {            boolean            result=imitation;         String inputstring="Alive is awesome";         System.out.println("String method three answer "+ method3(inputstring));     }            public            static            boolean            method3(String input)     {         ArrayList ar=            new            ArrayList();            for            (int            i=0; i < input.length() ; i++ )         {            int            j = input.indexOf(input.charAt(i));             ar.add(j);         }         Collections.sort(ar);            for            (int            i=0;i < (ar.size()-1);i++)         {            if            (ar.get(i) == ar.get(i+1))            return            imitation;         }            render            truthful;     } }          

PseudoCode for Method 4 :

i. For this method we need to know near  two inbuilt functions in java , indexOf() which returns the index of starting time occurence of the graphic symbol in the string , while second office lastIndexOf() returns the index of last occurence of the character in the given cord.
2. First , we convert the given inputstring into characterarray by using toCharArray() function.
three. Calculate the indexOf() and lastIndexOf() for each character in the given inputstring
4. If both are equal then continue and brand effect= true
else set flag outcome = false
5. Render result

Method 4

            import            coffee.util.ArrayList;            import            java.util.Collections;            import            java.util.HashSet;            public            course            UniqueChar4            {            public            static            void            main            (String args[])     {            boolean            result=false;         String inputstring="Live is awesome";         System.out.println("String method four answer "+ method4(inputstring));     }            public            static            boolean            method4(String input)     {            boolean            result=false;            for            (char            ch:            input.toCharArray())         {            if(input.indexOf(ch)== input.lastIndexOf(ch))             effect=            true;            else            {                 upshot=imitation;            interruption;             }         }            return            event;     } }
          
        

Method v

Virtually memory efficient answer

Delight mention in the comments what is the time complexity of each of the above method and why ?
Also write in comments if you take any other method to detect all the unique characters in string.

jaegerstratiout.blogspot.com

Source: https://javahungry.blogspot.com/2014/11/string-has-all-unique-characters-java-example.html

0 Response to "How to Read Unique Character in a String Java"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel