Skip to content

Commit 58d99a0

Browse files
committed
Sync LeetCode submission Runtime - 3 ms (14.87%), Memory - 16.6 MB (18.77%)
1 parent a2fcc81 commit 58d99a0

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

2580-circular-sentence/README.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<p>A <strong>sentence</strong> is a list of words that are separated by a<strong> single</strong> space with no leading or trailing spaces.</p>
2+
3+
<ul>
4+
<li>For example, <code>&quot;Hello World&quot;</code>, <code>&quot;HELLO&quot;</code>, <code>&quot;hello world hello world&quot;</code> are all sentences.</li>
5+
</ul>
6+
7+
<p>Words consist of <strong>only</strong> uppercase and lowercase English letters. Uppercase and lowercase English letters are considered different.</p>
8+
9+
<p>A sentence is <strong>circular </strong>if:</p>
10+
11+
<ul>
12+
<li>The last character of a word is equal to the first character of the next word.</li>
13+
<li>The last character of the last word is equal to the first character of the first word.</li>
14+
</ul>
15+
16+
<p>For example, <code>&quot;leetcode exercises sound delightful&quot;</code>, <code>&quot;eetcode&quot;</code>, <code>&quot;leetcode eats soul&quot; </code>are all circular sentences. However, <code>&quot;Leetcode is cool&quot;</code>, <code>&quot;happy Leetcode&quot;</code>, <code>&quot;Leetcode&quot;</code> and <code>&quot;I like Leetcode&quot;</code> are <strong>not</strong> circular sentences.</p>
17+
18+
<p>Given a string <code>sentence</code>, return <code>true</code><em> if it is circular</em>. Otherwise, return <code>false</code>.</p>
19+
20+
<p>&nbsp;</p>
21+
<p><strong class="example">Example 1:</strong></p>
22+
23+
<pre>
24+
<strong>Input:</strong> sentence = &quot;leetcode exercises sound delightful&quot;
25+
<strong>Output:</strong> true
26+
<strong>Explanation:</strong> The words in sentence are [&quot;leetcode&quot;, &quot;exercises&quot;, &quot;sound&quot;, &quot;delightful&quot;].
27+
- leetcod<u>e</u>&#39;s&nbsp;last character is equal to <u>e</u>xercises&#39;s first character.
28+
- exercise<u>s</u>&#39;s&nbsp;last character is equal to <u>s</u>ound&#39;s first character.
29+
- soun<u>d</u>&#39;s&nbsp;last character is equal to <u>d</u>elightful&#39;s first character.
30+
- delightfu<u>l</u>&#39;s&nbsp;last character is equal to <u>l</u>eetcode&#39;s first character.
31+
The sentence is circular.</pre>
32+
33+
<p><strong class="example">Example 2:</strong></p>
34+
35+
<pre>
36+
<strong>Input:</strong> sentence = &quot;eetcode&quot;
37+
<strong>Output:</strong> true
38+
<strong>Explanation:</strong> The words in sentence are [&quot;eetcode&quot;].
39+
- eetcod<u>e</u>&#39;s&nbsp;last character is equal to <u>e</u>etcode&#39;s first character.
40+
The sentence is circular.</pre>
41+
42+
<p><strong class="example">Example 3:</strong></p>
43+
44+
<pre>
45+
<strong>Input:</strong> sentence = &quot;Leetcode is cool&quot;
46+
<strong>Output:</strong> false
47+
<strong>Explanation:</strong> The words in sentence are [&quot;Leetcode&quot;, &quot;is&quot;, &quot;cool&quot;].
48+
- Leetcod<u>e</u>&#39;s&nbsp;last character is <strong>not</strong> equal to <u>i</u>s&#39;s first character.
49+
The sentence is <strong>not</strong> circular.</pre>
50+
51+
<p>&nbsp;</p>
52+
<p><strong>Constraints:</strong></p>
53+
54+
<ul>
55+
<li><code>1 &lt;= sentence.length &lt;= 500</code></li>
56+
<li><code>sentence</code> consist of only lowercase and uppercase English letters and spaces.</li>
57+
<li>The words in <code>sentence</code> are separated by a single space.</li>
58+
<li>There are no leading or trailing spaces.</li>
59+
</ul>

2580-circular-sentence/solution.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Approach 2: Space-optimized
2+
3+
# Time: O(n)
4+
# Space: O(1)
5+
6+
class Solution:
7+
def isCircularSentence(self, sentence: str) -> bool:
8+
for i in range(len(sentence)):
9+
if sentence[i] == ' ' and sentence[i-1] != sentence[i+1]:
10+
return False
11+
return sentence[0] == sentence[-1]
12+

0 commit comments

Comments
 (0)