Check if two strings are permutations of each other Description:

Given two strings check if those two are permutation of each other. Its expected that input string can contain ASCII characters.

Code:

class Main {
  public static boolean isPermutation(String s1, String s2) {
    if (s1 == null || s2 == null) {
      throw new IllegalArgumentException("Input string cannot be null");
    }
    if (s1.length() != s2.length()) {
      return false;
    }
    int[] count = new int[256];
    for (int i = 0; i &lt; s1.length(); i++) {
      int index = s1.charAt(i) - '0';
      count[index] += 1;
    }
    for (int i = 0; i &lt; s2.length(); i++) {
      int index = s2.charAt(i) - '0';
      count[index] -= 1;
    }
    for (int i = 0; i &lt; count.length; i++) {
      if (count[i] != 0) return false;
    }
    return true;  
  }

  public static void main(String[] args) {
    //System.out.println(isPermutation(null, null) == true);
    System.out.println(isPermutation("", "") == true);
    System.out.println(isPermutation("a", "") == false);
    System.out.println(isPermutation("", "b") == false);
    System.out.println(isPermutation("a", "a") == true);
    System.out.println(isPermutation("a", "b") == false);
    System.out.println(isPermutation("foo", "bar") == false);
    System.out.println(isPermutation("eat", "ate") == true);
    System.out.println(isPermutation("1010", "1100") == true);
  }
}


Questions:


From interview point of view are the test cases sufficient?
am I using Java features correctly?
I think I can merge the two for loops but I did above coding in time
bounded fashion, can it raise red flag from interviewer perspective?


PS: According to me I can use map if the input string can contain unicode characters.
Test cases

From an interview perspective, I would suggest that you're missing the test case for the null-handling. Creating full unit test cases in an interview is overkill, but is it too much to just add:

try {
    System.out.println("Should not happen: " + isPermutation(null, ""));
} catch (IllegalArgumentException e) {
    System.out.println("Got expected exception for null");
}


Java features

Your use of Java features is OK, but you are not really doing a whole lot.

Bugs

Your spec says the strings will contain ASCII characters, and, to me, things like spaces  and punctuation -_!@#$%.... are all ASCII. Your code subtracts the 0 character value from the string's character to get the array index. This will fail for the spaces and punctuation. Why do you need to do the 0 subtraction at all? The code would work for all ASCII without that anyway.

Algorithm

My biggest suggestion is about the algorithm, anyway. Your code counts each character and compares the counts. A much simpler, and intuitive algorithm, is to sort the characters and compare the results.....

Consider:

private static String normalize(String value) {
    char[] chars = value.toCharArray();
    Arrays.sort(chars);
    return new String(chars);
}


public static boolean isPermutation(String s1, String s2) {
    if (s1 == null || s2 == null) {
      throw new IllegalArgumentException("Input string cannot be null");
    }
    return normalize(s1).equals(normalize(s2));
}