Một câu được gọi là smooth khi chữ cái đầu của mỗi từ trùng với chữ cái cuối của từ trước nó.
Ví dụ
'this stringt tries sto obe esmooth'→ true (t→t, s→s, t→t, o→o, e→e)'this string is not smooth'→ false
Giải pháp
Mã triển khai:
const str = 'this stringt tries sto obe esmooth';
const str2 = 'this string is not smooth';
const isSmooth = str => {
const words = str.split(' ');
for (let i = 0; i < words.length - 1; i++) {
const current = words[i];
const next = words[i + 1];
if (current[current.length - 1] !== next[0]) return false;
}
return true;
};
console.log(isSmooth(str)); // true
console.log(isSmooth(str2)); // false
Kết quả
Đầu ra trong console:
true
false