Interview questions. Determine whether each other is a character rearrangement

Given two strings of lowercase letters s1and s2, write a program to determine if the characters of one string can be rearranged to make the other string.

Example 1:

Input: s1 = "abc", s2 = "bca" 
Output: true

Example 2:

Input: s1 = "abc", s2 = "bad" 
Output: false

code show as below:

class Solution {
public:
    bool CheckPermutation(string s1, string s2) {
        if(s1.size()!=s2.size())
        {
            return false;
        }
        sort(s1.begin(),s1.end());//将两个字符串进行排序,看排序后的两个字符串是否相等
        sort(s2.begin(),s2.end());
        return s1==s2;

    }
};

Guess you like

Origin blog.csdn.net/m0_62379712/article/details/132206530