topic:
Given two strings s1 and s2, Write a program, which determines the character of a string rearranging, you can become a another 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
Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/check-permutation-lcci
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.
Problem-solving ideas:
1 by means of an auxiliary array of size range of the ASCII code, the initial value of the array 0
2. Traversal string s1, the number of recording elements occurring
3. Traversal string s2, if the current element in the array is 0, which does not appear in the over-s1, false is returned; otherwise, this is the number of elements minus 1
4. s2 traverse end, return true
Code:
class Solution {
public:
bool CheckPermutation(string s1, string s2) {
int len1=s1.length();
int len2=s2.length();
int num[200]={0};
for(int i=0;i<len1;++i){
num[s1[i]-'0']++;
}
for(int j=0;j<len2;++j){
if(num[s2[j]-'0']<1){
return false;
}
num[s2[j]-'0']--;
}
return true;
}
};
Perform memory and time-consuming occupation situation: