Saturday, November 9, 2019

How To Decide If A String Has All Unique Characters Inwards Java?

This is 1 of the mutual string based coding occupation from programming project interviews. You demand to write a computer program to create upward one's heed if a given string has all unique characters or not. For representative input= "Java" together with hence your business office should render faux because all characters are non unique, together with if the input is "Python" together with hence your computer program should render truthful because all characters inward Python are unique. For the utilisation of this problem, you lot tin assume the given String alone contains ASCII printable characters, though you lot should ever verify that alongside the interviewer. You tin too assume that your solution needs to case-sensitive i.e. "P" together with "p" volition live on considered ii dissimilar characters together with a string containing a missive of the alphabet inward both majuscule together with the modest representative volition live on considered unique. You are too gratis to solve the occupation inward house of using whatever additional information structure.

The outset solution which comes to heed to solve this occupation is past times using a Set information structure. Influenza A virus subtype H5N1 Set is an unordered collection which doesn't allow duplicates. The JDK provides several Set implementations e.g. HashSet, TreeSet or LinkedHashSet simply to solve this occupation nosotros tin utilisation full general utilisation HashSet. The add() method is used to insert an chemical component into Set together with this method returns truthful if an chemical component is successfully inserted otherwise it returns faux i.e. when an chemical component is already introduce inward the Set.

If nosotros larn through each grapheme of String together with insert them into Set using add() method together with hence nosotros tin uncovering if all characters of String are unique or not. If at that spot are whatever duplicate characters together with hence add() volition render faux together with nosotros tin halt our computer program returning that all characters of String are non unique.

Here is the exact algorithm to solve this problem:

1) Create a Set e.g. HashSet
2) Get all characters of String using chars()
3) loop over all characters together with insert into Set 1 at a time
4) If add() method render faux together with hence terminate the computer program because non all characters are unique.
5) If all characters are successfully inserted together with hence render truthful because all characters of String is unique



Program to create upward one's heed if a string has all unique characters inward Java

Here is a Java computer program to convert to a higher house algorithm into code:

 This is 1 of the mutual string based coding occupation from programming project interviews How to create upward one's heed if a string has all unique characters inward Java?import java.util.HashSet; import java.util.Scanner; import java.util.Set;  /*  * Java Program to depository fiscal establishment tally if all characters of String are unique.   */ public class Demo {    public static void main(String[] args) throws Exception {      // create Scanner to read user input     Scanner sc = new Scanner(System.in);      System.out.println("Please locomote inward a String");     String input = sc.nextLine();      if (isUnique(input)) {       System.out.println("All characters of String are unique");     } else {       System.out.println("All characters of String are non unique");     }      sc.close();   }    /**    * Returns truthful if all characters of given String are unique    *     * @param input    * @return truthful if no duplicate characters    */   public static boolean isUnique(String input) {     // Create a Set to insert characters     Set<Character> set = new HashSet<>();      // larn all characters shape String     char[] characters = input.toCharArray();      for (Character c : characters) {       if (!set.add(c)) {         return false;       }     }     return true;   } } Output Please enter a String Java All characters of String are not unique Please enter a String Python All characters of String are unique

From the output, it's clear that our computer program is working fine. When the user enters "Java" it returns that all characters are non unique because "a" is repeated inward "Java", simply when the user enters "Python" it prints "all characters are unique" because at that spot is no duplicate grapheme inward "Python".

That's all virtually how to depository fiscal establishment tally if given string has all unique characters. As I said, at that spot are multiple ways to create it e.g. you lot tin create it inward place, or you lot tin utilisation additional information construction e.g. laid or hash tabular array to create upward one's heed if a string has all unique characters. This occupation is too asked equally for how to depository fiscal establishment tally if String contains duplicates or finding the repeated characters from the string. You tin utilisation the same technique to solve those problems equally well.

Further Learning
The Coding Interview Bootcamp: Algorithms + Data Structures
Data Structures together with Algorithms: Deep Dive Using Java
Algorithms together with Data Structures - Part 1 together with 2

Thanks for reading this article hence far. If you lot similar this interview query together with my explanation together with hence delight portion alongside your friends together with colleagues. If you lot accept whatever doubtfulness together with hence delight drib a comment.

No comments:

Post a Comment