Solved a String Permutation Problem in Java

Sarthak Kulkarni - Oct 25 - - Dev Community

Question:

Given two strings A and B, check if they can be rearranged to match (i.e., check if they are permutations of each other).

My Solution:

import java.util.Arrays;

`public class Solution {
    public int permuteStrings(String A, String B) {
        if (A == null || B == null || A.length() != B.length()) {
            return 0;
        }
        char A1[] = A.toCharArray();
        Arrays.sort(A1);
char B1[] = B.toCharArray();
Arrays.sort(B1);
return Arrays.equals(A1, B1) ? 1 : 0;
}
}`
Enter fullscreen mode Exit fullscreen mode

References:

W3Schools on equals()
GeeksforGeeks on Sorting Characters

What I Learned:

The difference between reference and content equality in Java.
Importance of input validation and handling edge cases.

.