Last active
August 29, 2015 14:02
-
-
Save jyhjuzi/6a48769758a67ee2a7ae to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.HashMap; | |
import java.util.Arrays; | |
public class Q1_3{ | |
public static void main(String args[]){ | |
String str1= "123gefrfd"; | |
String str2= "231gerfdd"; | |
System.out.println(isPermutation1(str1, str2)); | |
} | |
public static boolean isPermutation2(String str1, String str2){ | |
if(str1.length()!=str2.length()) | |
return false; | |
char[] array1 = str1.toCharArray(); | |
char[] array2 = str2.toCharArray(); | |
Arrays.sort(array1); | |
Arrays.sort(array2); | |
for( int i = 0; i < array1.length; i++){ | |
if(array1[i] != array2[i]) | |
return false; | |
} | |
return true; | |
} | |
public static boolean isPermutation1(String str1, String str2){ | |
if(str1.length()!=str2.length()) | |
return false; | |
HashMap<Character,Integer> map = new HashMap<Character,Integer>(); | |
for(int i =0; i <str1.length();i++){ | |
Character c = str1.charAt(i); | |
if(map.containsKey(c)) | |
map.put(c, map.get(c)+1); | |
else map.put(c,1); | |
} | |
for(int i = 0; i<str2.length(); i++){ | |
Character c = str2.charAt(i); | |
if(map.containsKey(c)){ | |
if(map.get(c)>0) | |
map.put(c, map.get(c)-1); | |
else return false; | |
} | |
else return false; | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment