Đề bài
Viết hàm nhận hai chuỗi str1, str2. Trả về true nếu một phần ký tự của str1 có thể sắp xếp lại thành str2, ngược lại false.
Ví dụ
const str1 = 'rkqodlw';
const str2 = 'world';
const canForm = (str1 = '', str2 = '') => {
if (str1.length < str2.length) return false;
const res = str2.split('');
str1.split('').forEach(val => {
if (res.includes(val)) {
res.splice(res.indexOf(val), 1);
}
});
return res.length === 0;
};
console.log(canForm(str1, str2)); // true
Kết quả
true