Class Solution

java.lang.Object
g2401_2500.s2490_circular_sentence.Solution

public class Solution extends Object
2490 - Circular Sentence.<p>Easy</p> <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> <ul> <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> </ul> <p>Words consist of <strong>only</strong> uppercase and lowercase English letters. Uppercase and lowercase English letters are considered different.</p> <p>A sentence is <strong>circular</strong> if:</p> <ul> <li>The last character of a word is equal to the first character of the next word.</li> <li>The last character of the last word is equal to the first character of the first word.</li> </ul> <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> <p>Given a string <code>sentence</code>, return <code>true</code> <em>if it is circular</em>. Otherwise, return <code>false</code>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> sentence = &ldquo;leetcode exercises sound delightful&rdquo;</p> <p><strong>Output:</strong> true</p> <p><strong>Explanation:</strong> The words in sentence are [&ldquo;leetcode&rdquo;, &ldquo;exercises&rdquo;, &ldquo;sound&rdquo;, &ldquo;delightful&rdquo;].</p> <ul> <li>leetcod<ins>e</ins>&rsquo;s last character is equal to <ins>e</ins>xercises&rsquo;s first character.</li> <li>exercise<ins>s</ins>&rsquo;s last character is equal to <ins>s</ins>ound&rsquo;s first character.</li> <li>soun<ins>d</ins>&rsquo;s last character is equal to <ins>d</ins>elightful&rsquo;s first character.</li> <li>delightfu<ins>l</ins>&rsquo;s last character is equal to <ins>l</ins>eetcode&rsquo;s first character.</li> </ul> <p>The sentence is circular.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> sentence = &ldquo;eetcode&rdquo;</p> <p><strong>Output:</strong> true</p> <p><strong>Explanation:</strong> The words in sentence are [&ldquo;eetcode&rdquo;].</p> <ul> <li>eetcod<ins>e</ins>&rsquo;s last character is equal to <ins>e</ins>etcode&rsquo;s first character. The sentence is circular.</li> </ul> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> sentence = &ldquo;Leetcode is cool&rdquo;</p> <p><strong>Output:</strong> false</p> <p><strong>Explanation:</strong> The words in sentence are [&ldquo;Leetcode&rdquo;, &ldquo;is&rdquo;, &ldquo;cool&rdquo;]. - Leetcod<ins>e</ins>&rsquo;s last character is <strong>not</strong> equal to <ins>i</ins>s&rsquo;s first character. The sentence is <strong>not</strong> circular.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= sentence.length <= 500</code></li> <li><code>sentence</code> consist of only lowercase and uppercase English letters and spaces.</li> <li>The words in <code>sentence</code> are separated by a single space.</li> <li>There are no leading or trailing spaces.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • isCircularSentence

      public boolean isCircularSentence(String sentence)