
Hi,
please share the code to check, if two strings are permutations of each other in java.

Here is an example that prompts the user to enter two strings in order to check whether they are permutation of each other or not.
import java.util.*;
class CheckPermutation{
public static boolean isPermutation(String st1, String st2) {
if (st1.length() != st2.length()) {
return false;
}
int[] count = new int[256];
for (int i = 0; i < st1.length(); ++i) {
++count[st1.charAt(i)];
}
for (int i = 0; i < st2.length(); ++i) {
if (--count[st2.charAt(i)] < 0) {
return false;
}
}
return true;
}
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
System.out.print("Enter string1: ");
String st1=input.next();
System.out.print("Enter string2: ");
String st2=input.next();
if(isPermutation(st1,st2)){
System.out.println("Strings are permutations of each other.");
}
else{
System.out.println("Strings are not permutations of each other.");
}
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.