Last active
August 29, 2015 14:18
-
-
Save LinyinWu/deddefd0cb379783e61b 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
/* | |
【解题思路】 | |
不考虑空白,区分大小写,创建一个256的数组来储存第一个字符串里的字符, 再对第二个字符串遍历看是否一样。 | |
【时间复杂度】 | |
O(n) | |
【空间复杂度】 | |
O(1) | |
【gist link] | |
https://gist.github.com/LinyinWu/deddefd0cb379783e61b | |
【test case】 | |
"abc"; | |
"bca"; | |
"acd"; | |
"cab"; | |
*/ | |
public class Solution { | |
public static boolean permutation(String s1, String s2) { | |
if(s1.length() != s2.length()) | |
return false; | |
int[] asc = new int[256]; | |
for(int i= 0;i<s1.length();i++) { | |
int index = s1.charAt(i); | |
asc[index]++; | |
} | |
for(int i=0;i<s2.length();i++) { | |
int index = s2.charAt(i); | |
if(--asc[index] < 0) | |
return false; | |
} | |
return true; | |
} | |
public static void main(String[] args) { | |
String s1 = "abc"; | |
String s2 = "bca"; | |
String s3 = "acd"; | |
String s4 = "cab"; | |
boolean a = permutation(s1,s2); | |
System.out.println(a); | |
boolean b = permutation(s1,s3); | |
System.out.println(b); | |
boolean c = permutation(s1,s4); | |
System.out.println(c); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment