Created
June 18, 2014 05:14
-
-
Save larry-liu/c3ec4c9f7f5586864963 to your computer and use it in GitHub Desktop.
CC1.3
This file contains hidden or 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.io.*; | |
import java.util.*; | |
class permutation { | |
public static void main(String[] args) { | |
String str1 = "abc"; | |
String str2 = "cba"; | |
if(permutation(str1, str2)) | |
System.out.println("They are each other's permutation"); | |
else | |
System.out.println("They are not each other's permutation"); | |
} | |
public static boolean permutation(String str1, String str2) { | |
int[] count = new int[256]; | |
int len1 = str1.length(); | |
int len2 = str2.length(); | |
if(len1 != len2) | |
return false | |
boolean success =true; | |
for(int i = 0; i < len1; i++) { | |
count[str1.charAt(i)] ++; | |
} | |
for(int i = 0; i < len2; i ++) { | |
count[str1.charAt(i)] --; | |
} | |
for(int i = 0; i < 256; i ++) { | |
if(count[i] != 0) | |
success = false; | |
} | |
return success; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A small mistake at line 25, “str1.charAt(i)” should be "str2.charAt(i)", otherwise it always returns "true" when “len1 == len2”.