Unique String

A String is said to be ‘Unique’ if none of the alphabets resent in the string are repeated.  Write a program to accept a string and check whether the string is Unique or not.  

Example :-

Input: COMPUTER

Output:Unique String

Input: APPLICATIONS

Output: Not a Unique String

import java.util.Scanner;
class Unique
{
    public static void main(String args[])
    {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter the string");
        String str = in.nextLine();
        str=str.toUpperCase();
        int l=str.length();
        int n=0; //counter variable to count repetitions
        for(int i=0; i<l-1; i++)
        {
            char ch=str.charAt(i);
            for(int j=i+1; j<l; j++)
            {
                char ch1=str.charAt(j);
                if(ch==ch1)
                {
                    n++;
                    break;
                }
            }
        }
        
        if(n>=1)
            System.out.println("It is not a unique string");
        else
            System.out.println("It is a unique string");
    }
}
Video explanation

Leave a Comment

Your email address will not be published. Required fields are marked *