Check if the given string has duplicates Description:

Given a string find if it contains duplicate characters. The string contains only ASCII characters.

Code:

import java.util.Arrays;

class Main {
  /**
   * Given a string check if it duplicate characters.
   * []        -&gt; true
   * [f, o, o] -&gt; false
   * [b, a, r] -&gt; true
   * 
   * @param {String} str
   * 
   * @return false if string contains duplicate else true
   */ 
  public static boolean isUnique(String str) {
    if (str.length() == 0) {
      return true;
    }
    boolean[] seen = new boolean[25];
    for (int i = 0; i &lt; str.length(); i++) {
      int index = Character.toLowerCase(str.charAt(i)) - 'a';
      if (seen[index]) {
        return false;
      }
      else {
        seen[index] = true;
      }
    }
    // invariant
    return true;
  }

  public static void main(String[] args) {
    System.out.println(isUnique(""));       // true
    System.out.println(isUnique("a"));      // true
    System.out.println(isUnique("foo"));    // false
    System.out.println(isUnique("bar"));    // true
    System.out.println(isUnique("hello"));  // false
    System.out.println(isUnique("Hello"));  // false
  }
}


The above algorithms runs in O(n), the alternate solution is to sort the string and the check the adjacent elements but it would be O(nlogn). I need to understand if I am using Java correctly, any other styles related comments are most welcome.

PS: I would like to know if I need to include more tests to cover all cases.
Documentation mismatch

The documentation and the functionality do not fit. Your function is supposed to "check if [a string] has duplicates". From the documentation, I expect a function hasDuplicates that returns true if I have duplicate characters and false otherwise.

However, you provide isUnique, which does the opposite: return true if there are no duplicates and false if there are duplicates. The internal documentation fits, but I would say "checks whether it contains no duplicate characters" or "contains unique characters only".

Possible bugs

You checked your string only on alphabetic strings. What happens on "12345678"? You will index -48 due to '1' - 'a'. That's a bug waiting to happen. Also, what happens if your string contains a 'z'? What happens if str is null? Maybe str shouldn't be null, but that should get mentioned in the documentation (see above).

Also, you should mention that your function is case-insensitive. But why does "Aa" count as a duplicate to begin with?

Both cases (non-alpha character and same letters in different cases) should get added to your tests.

Algorithm

Your seen algorithm is fine if you know exactly how many buckets you need. But you often don't know that. A Map&lt;char,bool&gt; can happen here. If you use a TreeMap you end up with the mentioned \$\mathcal O(n \log n)\$, but if you use a HashMap the asymptotic complexity is \$\mathcal O(n)\$ again. Alternatively, use a set.

But that's asymptotic complexity. If you don't consider any non-alpha strings (see section above), then you can immediately return false if the string is longer than 26 characters due to the pigeon hole principle.