fork download
  1. public class Main {
  2.  
  3. public static void main(String[] args) {
  4. System.out.println(isPermute("cat", "tca"));
  5. System.out.println(isPermute("ssh", "ssi"));
  6. System.out.println(isPermute("gdg", "gsg"));
  7. }
  8.  
  9. static boolean isPermute(String x, String y) {
  10. if (x.length() != y.length()) {
  11. return false;
  12. }
  13.  
  14. int length = x.length();
  15. boolean[] alreadyMatched = new boolean[length];
  16.  
  17. for (int i = 0; i < length; i++) {
  18. boolean matched = false;
  19.  
  20. for (int j = 0; j < length; j++) {
  21. if (!alreadyMatched[j]) {
  22. if (x.charAt(i) == y.charAt(j)) {
  23. alreadyMatched[j] = true;
  24. matched = true;
  25. break;
  26. }
  27. }
  28. }
  29.  
  30. if (!matched) {
  31. return false;
  32. }
  33. }
  34.  
  35. return true;
  36. }
  37. }
Success #stdin #stdout 0.07s 380160KB
stdin
Standard input is empty
stdout
true
false
false