Is String containing unique letter - Java Algorithm

Java examples for Algorithm:String

Description

Is String containing unique letter

Demo Code


public class Main {
  public boolean isUnique(String line) {
    // edge cases: null, 0, >256
    if (line == null)
      return false;
    int len = line.length();
    if (len == 0)
      return true;
    if (len > 256)
      return false;
    // count as a flag, if true ==> it was seen before
    boolean[] count = new boolean[256];
    for (int i = 0; i < len; i++) {
      char c = line.charAt(i);
      if (count[c])
        return false;
      else/*from ww  w. ja v  a2 s  .  c  o m*/
        count[c] = true;
    }
    return true;
  }

  public static void main(String[] args) {
    String line = "fskfhsjkfhjskhfweryiuwryiwu";
    System.out.print(new Main().isUnique(line));
  }

}

Related Tutorials