String Permutation

String Permutation

 
Write an algorithm to check if a string is a permutation of the given string.
 
Given String:  gupta
 
Input:  atugp
 
Output : true.
Since input1 is string permutation of the given string.
 
Here is the code.
 



public class StringPermutation {
public static boolean isPermutation(String s1, String s2) {
if (s1.length() != s2.length()) {
return false;
}
int[] letters = new int[128];
for (char c : s1.toCharArray()) {
letters[c]++;
}
for (char c : s2.toCharArray()) {
letters[c]--;
if (letters[c] < 0) {
return false;
}
}
return true;
}
public static void main(String[] args) {
System.out.println(isPermutation("gupta", "atugp"));
}
}


 
Output:






No Comments Yet

Leave a Reply

Your email address will not be published.

Lorem ipsum dolor sit amet, consectetur a dipiscing elit. Vivamus leo ante,

FOLLOW US ON