#2490

Circular Sentence

newbie · 280 · lc easy +20 · verified · 70.2% accepted · 762 likes · top 79%

Description

A sentence is a space-separated list of words with no leading or trailing spaces. A sentence is circular when the last character of every word matches the first character of the next word, and the last character of the final word matches the first character of the first word (treating uppercase and lowercase as distinct). Given a string sentence, return true if it is circular, otherwise return false.

Example 1:

Input: sentence = "leetcode exercises sound delightful"
Output: true
Explanation: The words in sentence are ["leetcode", "exercises", "sound", "delightful"].
- leetcode's last character is equal to exercises's first character.
- exercises's last character is equal to sound's first character.
- sound's last character is equal to delightful's first character.
- delightful's last character is equal to leetcode's first character.
The sentence is circular.

Example 2:

Input: sentence = "eetcode"
Output: true
Explanation: The words in sentence are ["eetcode"].
- eetcode's last character is equal to eetcode's first character.
The sentence is circular.

Example 3:

Input: sentence = "Leetcode is cool"
Output: false
Explanation: The words in sentence are ["Leetcode", "is", "cool"].
- Leetcode's last character is not equal to is's first character.
The sentence is not circular.

Code

1
2
3