Given two strings str1 and str2 as input, check whether the strings are a permutation of each other. str1 is a permutation of str2 if all the characters of str2 can be arranged in some way to form str1.
isPermutation("how","who")=true
isPermutation("1234","43210")=false
Please comment if you find any mistake.
isPermutation("how","who")=true
isPermutation("1234","43210")=false
public class PermutationString {
static String testcase1 = "12341";
static String testcase2 = "43211";
public static void main(String args[]){
PermutationString
testInstance= new PermutationString();
boolean result =
testInstance.isPermutation(testcase1,testcase2);
System.out.println("Result :
"+result);
}
//write your code
here
public boolean isPermutation(String
str1,String str2){
int
lenstr1=str1.length();
int len2str2=str2.length();
if(lenstr1!=len2str2)
{
return false;
}
for(int
iteration=0;iteration<lenstr1;iteration++)
{
char
ch=str1.charAt(iteration);
int
count1=countingChar(ch,str1);
int
count2=countingChar(ch,str2);
if(count1!=count2)
{
return false;
}
}
return true;
}
//counting number
of a particular character in string
private int countingChar(char ch, String str) {
int count=0;
for(int
iteration=0;iteration<str.length();iteration++)
{
char
cha=str.charAt(iteration);
if(ch==cha)
{
count++;
}
}
return count;
}
}
Please comment if you find any mistake.
No comments:
Post a Comment