DEV Community

Sarthak Kulkarni
Sarthak Kulkarni

Posted on

Solved a String Permutation Problem in Java

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.

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post