Determines whether intercharacter rearrangement is possible
Given two strings s1 and s2, write a program that determines whether the characters of one string can be rearranged into the other string.
Example 1:
Input: s1 = ABC, s2 = bCA Output: true Example 2:
Input: s1 = ABC, s2 = bad Output: false Description:
0 <= len(s1) <= 100
0 <= len(s2) <= 100
python
class Solution(object) :
def CheckPermutation(self, s1, s2) :
s11=sorted(s1)
s22=sorted(s2)
if s11==s22:
return True
return False
Copy the code
java
class Solution {
public boolean CheckPermutation(String s1, String s2) {
char s11[] = s1.toCharArray();
char s21[] = s2.toCharArray();
Arrays.sort(s11);
Arrays.sort(s21);
if(s11.length! =s21.length){return false;
}
for(int i = 0; i<s11.length; i++){if(s11[i]! =s21[i]){return false; }}return true; }}Copy the code
The idea is to sort the string, compare the two strings are equal, Java needs to traverse comparison, cold unknown details.