private String sort(String word) {
char[] content = word.toCharArray();
Arrays.sort(content);
return new String(content);
}
private boolean isPermutation(String s1, String s2) {
s2 = s2.toLowerCase();
if (s1.length() != s2.length()) {
return false;
}
if (s1.equals(s2)) {
return true;
}
return sort(s1).equals(sort(s2));
}
Could the above solution can be optimized(in space and time)?
I think the worst case running time would be: \$O(nlog)\$
toLowerCase
, since it has nothing to do with permutations. \$\endgroup\$