public class Main {
public static void main
(String[] args
) { System.
out.
println(isPermute
("cat",
"tca")); System.
out.
println(isPermute
("ssh",
"ssi")); System.
out.
println(isPermute
("gdg",
"gsg")); }
if (x.length() != y.length()) {
return false;
}
int length = x.length();
boolean[] alreadyMatched = new boolean[length];
for (int i = 0; i < length; i++) {
boolean matched = false;
for (int j = 0; j < length; j++) {
if (!alreadyMatched[j]) {
if (x.charAt(i) == y.charAt(j)) {
alreadyMatched[j] = true;
matched = true;
break;
}
}
}
if (!matched) {
return false;
}
}
return true;
}
}
cHVibGljIGNsYXNzIE1haW4gewoKICAgIHB1YmxpYyBzdGF0aWMgdm9pZCBtYWluKFN0cmluZ1tdIGFyZ3MpIHsKICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oaXNQZXJtdXRlKCJjYXQiLCAidGNhIikpOwogICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbihpc1Blcm11dGUoInNzaCIsICJzc2kiKSk7CiAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKGlzUGVybXV0ZSgiZ2RnIiwgImdzZyIpKTsKICAgIH0KICAgIAogICAgc3RhdGljIGJvb2xlYW4gaXNQZXJtdXRlKFN0cmluZyB4LCBTdHJpbmcgeSkgewogICAgICAgIGlmICh4Lmxlbmd0aCgpICE9IHkubGVuZ3RoKCkpIHsKICAgICAgICAgICAgcmV0dXJuIGZhbHNlOwogICAgICAgIH0KICAgICAgICAKICAgICAgICBpbnQgbGVuZ3RoID0geC5sZW5ndGgoKTsKICAgICAgICBib29sZWFuW10gYWxyZWFkeU1hdGNoZWQgPSBuZXcgYm9vbGVhbltsZW5ndGhdOwogICAgICAgIAogICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbGVuZ3RoOyBpKyspIHsKICAgICAgICAgICAgYm9vbGVhbiBtYXRjaGVkID0gZmFsc2U7CiAgICAgICAgICAgIAogICAgICAgICAgICBmb3IgKGludCBqID0gMDsgaiA8IGxlbmd0aDsgaisrKSB7CiAgICAgICAgICAgICAgICBpZiAoIWFscmVhZHlNYXRjaGVkW2pdKSB7CiAgICAgICAgICAgICAgICAgICAgaWYgKHguY2hhckF0KGkpID09IHkuY2hhckF0KGopKSB7CiAgICAgICAgICAgICAgICAgICAgICAgIGFscmVhZHlNYXRjaGVkW2pdID0gdHJ1ZTsKICAgICAgICAgICAgICAgICAgICAgICAgbWF0Y2hlZCA9IHRydWU7CiAgICAgICAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgIAogICAgICAgICAgICBpZiAoIW1hdGNoZWQpIHsKICAgICAgICAgICAgICAgIHJldHVybiBmYWxzZTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICAKICAgICAgICByZXR1cm4gdHJ1ZTsKICAgIH0KfQ==