Easiest way of checking if a string consists of unique characters?

前端 未结 12 1734
慢半拍i
慢半拍i 2021-01-04 23:34

I need to check in Java if a word consists of unique letters (case insensitive). As straight solution is boring, I came up with:

  1. For every char in a string che
相关标签:
12条回答
  • 2021-01-05 00:22

    By "unique letters" do you mean merely the standard English set of 26, or are you allowing interesting Unicode? What result do you expect if the string contains a non-letter?

    If you're only considering 26 possible letters, and you want to either ignore any non-letter or consider it an automatic fail, the best algorithm is likely this pseudocode:

    create present[26] as an array of booleans.
    set all elements of present[] to false.
    loop over characters of your string
      if character is a letter
        if corresponding element of present[] is true
          return false.
        else
          set corresponding element of present[] to true.
        end if
      else
        handle non-letters
      end if
    end loop
    

    The only remaining question is whether your array should actually be an array (requiring 26 operations to zero), or a bitfield (possibly requiring more work to check/set, but can be zeroed in a single operation). I think that the bitfield access will be pretty much comparable to the array lookup, if not faster, so I expect a bitfield is the right answer.

    0 讨论(0)
  • 2021-01-05 00:24
    public boolean hasUniqChars(String s){
      Hashset h = new Hashset();
      HashSet<Character> h = new HashSet<Character>();
      for (char c : s.toCharArray()) {
      if (!h.add(Character.toUpperCase(c))) // break if already present
        return false;
      }
      return true;
    }
    

    You should use hashset technique if you are performing char sets like utf-8 and for internationalization's sake.

    Javadoc on Character.toUpperCase for cases of utf: This method (toUpperCase(char) ) cannot handle supplementary characters. To support all Unicode characters, including supplementary characters, use the toUpperCase(int) method.

    0 讨论(0)
  • 2021-01-05 00:27

    First check if the size of string is <=26. If not , String has duplicates. return Else try adding into HashSet, if it fails, string has duplicates return. if size of HashSet is = size of string string has unique characters. If we are not allowed to use any other data structure, and string's internal methods and have to still do it in O(n), then loop thru the String.if i!=myLastIndexof(i), return Duplicates exist.

    0 讨论(0)
  • 2021-01-05 00:30
               import java.io.*;
    
                       class unique
                      {
                               public static int[] ascii(String s)
                               {
                                        int length=s.length();
                                        int asci[] = new int[length];
                                        for(int i=0;i<length;i++)
                                        {
                                                  asci[i]=(int)s.charAt(i);
                                         }
                                  return asci;
                                }
                                public static int[] sort(int a[],int l)
                               {
                                           int j=1,temp;
                                           while(j<=l-1)
                                           {
                                                     temp = a[j];
                                                      int k=j-1;
                                                      while(k>=0 && temp<a[k])
                                                     {
                                                               a[k+1]= a[k];
                                                               k--;
                                                     }
                                                    a[k+1]=temp;
                                                    j++;
                                           } 
                               return a;
                        }
                  public static boolean compare(int a[])
                { 
                         int length=a.length;
                         int diff[] = new int[length-1];
                         boolean flag=true;
                         for(int i=0;i<diff.length;i++)
                        {
                                 diff[i]=a[i]-a[i+1];
                                 if(diff[i]==0)
                                 {
                                            flag=false;
                                            break;
                                 }
                                 else
                                 {
                                          flag=true;
                                 }
                         }
                         return flag;
                    }
                    public static void main(String[] args)         throws IOException 
                   {
                     BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
                     String str = null;
                     boolean result = true;
                     System.out.println("Enter your String.....");
                     str = br.readLine();
                     str = str.toLowerCase();
                     int asc[]=ascii(str);
                     int len = asc.length;
                     int comp[]=sort(asc,len);
                     if(result==compare(comp))
                     {
                         System.out.println("The Given String is Unique");
                     }
                     else
                    {
                            System.out.println("The Given String is not Unique");
                     }
                  }
    

    }

    0 讨论(0)
  • 2021-01-05 00:32

    An improvement on option 2 is to check the boolean flag that the HashSet add method returns. It's true if the object wasn't already there. Though, for this method to be at all useful you'd first have to set the string to all caps or lowercase.

    0 讨论(0)
  • 2021-01-05 00:34

    What about using an int to store the bits corresponding to the index of the letter of the alpabhet? or maybe a long to be able to reach 64 distinct symbols.

    long mask;
    // already lower case
    string = string.toLowerCase();
    for (int i = 0; i < string.length(); ++i)
    {
      int index = 1 << string.charAt(i) - 'a';
      if (mask & index == index)
        return false;
    
      mask |= index;
    }
    return true;
    

    This should be < O(n) on average case, O(n) on worst. But I'm not sure how much performant bitwise operations are in Java..

    0 讨论(0)
提交回复
热议问题