# Coding Challenge Day-3: Problem 1: Check if two strings are permutation of each other

Hello! So I’m doing a 30 day coding challenge where I solve around 3-5 questions per day and thought of posting them here on my blog so that you guys can join the challenge too!

Welcome to Coding challenge Day 3: Problem one! Be sure to post your answers, queries etc in the comments!!

Problem: Check if two Strings are permutation of each other

Sample input: String1: “abcd”
String2: “dabc”
Output: Strings abcd and dabc are permutation Strings

Sample input: String1: “abcd”
String2: “drbc”
Output: Strings abcd and drbc are not permutation Strings

Sample input: String1: “TOMMARVOLORIDDLE”
String1:”IAMLORDVOLDEMORT”
Output: Strings TOMMARVOLORIDDLE and IAMLORDVOLDEMORT are permutation Strings

Solution:

• So First we find the length of both the strings and compare them
• If they are not the same then they are not permutation of each other
• If they are the same length then convert the strings to character array and sort both the arrays
• Compare each element of sorted array
``````package string;

import java.util.Arrays;
public class Program002 {

public static boolean permutation (String s1, String s2){
int length1= s1.length(), length2= s2.length();

if(length1!=length2)
return false;

char [] c1= s1.toCharArray();
char [] c2= s2.toCharArray();

Arrays.sort(c1);
Arrays.sort(c2);

for(int i=0; i<length1; i++){
if(c1[i]!=c2[i])
return false;
}

return true;

}

public static void main (String [] args){
String s1= "TOMMARVOLORIDDLE";
String s2="IAMLORDVOLDEMORT";

boolean b=permutation(s1,s2);

if(b)
System.out.printf("Strings %s and %s are permutation Strings",s1,s2);
else
System.out.printf("Strings %s and %s are not permutation Strings",s1,s2);

}

}
``````