Welcome to Subscribe On Youtube
Formatted question description: https://leetcode.ca/all/2490.html
2490. Circular Sentence
- Difficulty: Easy.
- Related Topics: .
- Similar Questions: Defuse the Bomb.
Problem
A sentence is a list of words that are separated by a** single** space with no leading or trailing spaces.
- For example,
"Hello World"
,"HELLO"
,"hello world hello world"
are all sentences.
Words consist of only uppercase and lowercase English letters. Uppercase and lowercase English letters are considered different.
A sentence is **circular **if:
-
The last character of a word is equal to the first character of the next word.
-
The last character of the last word is equal to the first character of the first word.
For example, "leetcode exercises sound delightful"
, "eetcode"
, "leetcode eats soul"
are all circular sentences. However, "Leetcode is cool"
, "happy Leetcode"
, "Leetcode"
and "I like Leetcode"
are not circular sentences.
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.
Constraints:
-
1 <= sentence.length <= 500
-
sentence
consist of only lowercase and uppercase English letters and spaces. -
The words in
sentence
are separated by a single space. -
There are no leading or trailing spaces.
Solution (Java, C++, Python)
-
class Solution { public boolean isCircularSentence(String sentence) { if (sentence.charAt(0) != sentence.charAt(sentence.length() - 1)) { return false; } String[] ss = sentence.split(" "); for (int i = 1; i < ss.length; ++i) { if (ss[i].charAt(0) != ss[i - 1].charAt(ss[i - 1].length() - 1)) { return false; } } return true; } }
-
class Solution { public: bool isCircularSentence(string sentence) { if (sentence[0] != sentence[sentence.size() - 1]) return false; istringstream is(sentence); vector<string> ss; string s; while (is >> s) ss.emplace_back(s); for (int i = 1; i < ss.size(); ++i) { if (ss[i][0] != ss[i - 1][ss[i - 1].size() - 1]) { return false; } } return true; } };
-
class Solution: def isCircularSentence(self, sentence: str) -> bool: sentence = sentence.split() return all(s[0] == sentence[i - 1][-1] for i, s in enumerate(sentence))
-
func isCircularSentence(sentence string) bool { if sentence[0] != sentence[len(sentence)-1] { return false } ss := strings.Split(sentence, " ") for i := 1; i < len(ss); i++ { if ss[i][0] != ss[i-1][len(ss[i-1])-1] { return false } } return true }
-
function isCircularSentence(sentence: string): boolean { const ss = sentence.split(' '); const n = ss.length; if (ss[0][0] !== ss[n - 1][ss[n - 1].length - 1]) { return false; } for (let i = 0; i < n - 1; i++) { if (ss[i][ss[i].length - 1] !== ss[i + 1][0]) { return false; } } return true; }
-
var isCircularSentence = function (sentence) { const words = sentence.split(' '); const post = words[0].charCodeAt(0); let prev = words[0].charCodeAt(words[0].length - 1); const n = words.length; for (let i = 1; i < n; i++) { let cur = words[i]; if (cur.charCodeAt(0) !== prev) { return false; } prev = cur.charCodeAt(cur.length - 1); } return post === prev; };
-
impl Solution { pub fn is_circular_sentence(sentence: String) -> bool { let ss: Vec<String> = sentence.split(' ').map(String::from).collect(); let n = ss.len(); if ss[0].as_bytes()[0] != ss[n - 1].as_bytes()[ss[n - 1].len() - 1] { return false; } for i in 1..n { if ss[i - 1].as_bytes()[ss[i - 1].len() - 1] != ss[i].as_bytes()[0] { return false; } } return true; } }
Explain:
nope.
Complexity:
- Time complexity : O(n).
- Space complexity : O(n).