给定两个字符串 s1 和 s2,请编写一个程序,确定其中一个字符串的字符重新排列后,能否变成另一个字符串。
示例 1:
输入: s1 = “abc”, s2 = “bca” 输出: true
示例 2:
输入: s1 = “abc”, s2 = “bad” 输出: false
说明:
0 <= len(s1) <= 100 0 <= len(s2) <= 100
来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/check-permutation-lcci 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
public boolean CheckPermutation(String s1
, String s2
) {
if (s1
.length() != s2
.length()) {
return false;
}
int size
= 52;
int[] alphabet_s1
= new int[size
];
for (char c
: s1
.toCharArray()) {
alphabet_s1
[getIndex(c
)]++;
}
int[] alphabet_s2
= new int[size
];
for (char c
: s2
.toCharArray()) {
alphabet_s2
[getIndex(c
)]++;
}
for (int i
= 0; i
< size
; i
++) {
if (alphabet_s1
[i
] != alphabet_s2
[i
]) {
return false;
}
}
return true;
}
private int getIndex(char c
) {
return c
- 'a';
}