给定两个由小写字母组成的字符串 s1
和 s2
,请编写一个程序,确定其中一个字符串的字符重新排列后,能否变成另一个字符串。
示例 1:
输入: s1 = "abc", s2 = "bca"
输出: true
示例 2:
输入: s1 = "abc", s2 = "bad"
输出: false
说明:
0 <= len(s1) <= 100
0 <= len(s2) <= 100
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public boolean CheckPermutation(String s1, String s2) {
if (s1 == null && s2 == null) {
return true;
}
if (s1 == null || s2 == null) {
return false;
}
if (s1.length() == 0 && s2.length() == 0) {
return true;
}
if (s1.length() != s2.length()) {
return false;
}
int[] arr1 = new int[26];
int[] arr2 = new int[26];
for (char ch : s1.toCharArray()) {
arr1[ch - 'a']++;
}
for (char ch : s2.toCharArray()) {
arr2[ch - 'a']++;
}
for (int i = 0; i < 26; i++) {
if (arr1[i] != arr2[i]) {
return false;
}
}
return true;
}
}
//leetcode submit region end(Prohibit modification and deletion)