Write an algorithm to determine if a string has all unique charcters.

public static boolean isUniqueChars2(String str)
{
       boolean [] char_set =new boolean[256];
          for(int i=0; i<str.length(); i++)
          {
              int val=str.charAt(i);
              if (char_set[val])
              return false;
             char_set[val]=true;
     
          }
           return true;
}

2nd Method :

public static boolean isUniqueChars(string str)
{
      int checker=0;
      for(int i=0;i<str.length();++i)
      {
          int val=str.charAt(i) -'a';
          if((checker & (1<<val))>0)
          return false;
          checker =(1<<val);
      }
      return true;
}

Comments

Popular posts from this blog

Show that number of pendant vertices in a binary tree is (n+1)/2 ,where n is the number of vertices in the tree.

Prove that every tree with 2 or more vertices is 2-chromatic.

Top 5 websites to learn coding Online