标签:ima 遍历 als 特殊 assign 不同 turn 位置 false
时间复杂度O(n2),空间复杂度应该是O(n)
class Solution {
public:
bool canConstruct(string ransomNote, string magazine) {
if(ransomNote.length() == 0) return true;
if(magazine.length() == 0 || ransomNote.length() > magazine.length()) return false;
vector<char> r;
r.resize(ransomNote.length());
r.assign(ransomNote.begin(), ransomNote.end());
vector<char> m;
m.resize(magazine.length());
m.assign(magazine.begin(), magazine.end());
sort(r.begin(), r.end());
sort(m.begin(), m.end());
for(int i = 0, record = 0; i < r.size(); i++){
if(r[i] == m[i+record]) continue;
else{
while(i + record < m.size() - 1 && r[i] != m[i+record]) record++;
if(r.size() - i > m.size() - i - record) return false;
if(r[i] != m[i+record]) return false;
}
}
return true;
}
};
标签:ima 遍历 als 特殊 assign 不同 turn 位置 false
原文地址:https://www.cnblogs.com/wasi-991017/p/12684941.html