Saturday, March 12, 2016

Check if a string hall all unique characters

Code:

using System;

public class Test
{
    public static void Main()
    {
        string str = "Asif";

        Console.WriteLine("Input: " + str);
       
        if(new Test().IsUniqueChar(str))
        {
         Console.WriteLine("Output: " + str + " have unique characters.");
        }
        else
        {
         Console.WriteLine("Output: " + str + " does not have unique characters.");
        }

       
    }

    public bool IsUniqueChar(string str)
    {
     int n = str.Length;
   
        for(int i = 0; i < n - 1 ; i++)
        {
         for(int j= i + 1; j < n; j++)
         {
          if(str.Substring(i,1) == str.Substring(j,1))
          {
           return false;
          }
         }
        }
       
        return true;
    }

}

Output:

Input: Asif
Output: Asif have unique characters.

Run Time: O(N^2)