标签:letter public describe print tran amp -- desc 排列
给定两个字符串,请编写程序,确定其中一个字符串的字符重新排列后,能否变成另一个字符串。这里规定大小写为不同字符,且考虑字符串重点空格。
给定一个string stringA和一个string stringB,请返回一个bool,代表两串是否重新排列后可相同。保证两串的长度都小于等于5000。
"This is nowcoder","is This nowcoder"
返回:true
"Here you are","Are you here"
返回:false
1 public static boolean checkSam(String stringA, String stringB) { 2 // write code here 3 char[] a = stringA.toCharArray(); 4 char[] b = stringB.toCharArray(); 5 java.util.Arrays.sort(a); 6 java.util.Arrays.sort(b); 7 stringA = a.toString(); 8 System.out.println(stringA); 9 stringB = b.toString(); 10 System.out.println(stringB); 11 return stringA.equals(stringB); 12 }
stringA = a.toString();???
char[] = string.toCharArray();
string = String.valueOf(char[]);
方法2:
1 public class Same { 2 public boolean checkSam(String stringA, String stringB) { 3 // write code here 4 if (stringA.length() != stringB.length()) return false; 5 char[] a = stringA.toCharArray(); 6 int[] count = new int[256]; 7 for (char c : a) { 8 count[c]++; 9 } 10 for (int i = 0; i < stringB.length(); i++) { 11 if (count[stringB.charAt(i)] == 0) { 12 return false; 13 } 14 else { 15 count[stringB.charAt(i)]--; 16 } 17 } 18 return true; 19 20 } 21 }
for (char c : a) {
count[c]++;
}
char c:a
count[c] 0 < c < 255
标签:letter public describe print tran amp -- desc 排列
原文地址:http://www.cnblogs.com/hafgyyb/p/6641877.html