|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# 2490. Circular Sentence |
| 4 | +# Easy |
| 5 | +# https://leetcode.com/problems/circular-sentence |
| 6 | + |
| 7 | +=begin |
| 8 | +A sentence is a list of words that are separated by a single space with no leading or trailing spaces. |
| 9 | +* For example, "Hello World", "HELLO", "hello world hello world" are all sentences. |
| 10 | +Words consist of only uppercase and lowercase English letters. Uppercase and lowercase English letters are considered different. |
| 11 | +A sentence is circular if: |
| 12 | +* The last character of a word is equal to the first character of the next word. |
| 13 | +* The last character of the last word is equal to the first character of the first word. |
| 14 | +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. |
| 15 | +Given a string sentence, return true if it is circular. Otherwise, return false. |
| 16 | +
|
| 17 | +Example 1: |
| 18 | +Input: sentence = "leetcode exercises sound delightful" |
| 19 | +Output: true |
| 20 | +Explanation: The words in sentence are ["leetcode", "exercises", "sound", "delightful"]. |
| 21 | +- leetcode's last character is equal to exercises's first character. |
| 22 | +- exercises's last character is equal to sound's first character. |
| 23 | +- sound's last character is equal to delightful's first character. |
| 24 | +- delightful's last character is equal to leetcode's first character. |
| 25 | +The sentence is circular. |
| 26 | +Example 2: |
| 27 | +Input: sentence = "eetcode" |
| 28 | +Output: true |
| 29 | +Explanation: The words in sentence are ["eetcode"]. |
| 30 | +- eetcode's last character is equal to eetcode's first character. |
| 31 | +The sentence is circular. |
| 32 | +Example 3: |
| 33 | +Input: sentence = "Leetcode is cool" |
| 34 | +Output: false |
| 35 | +Explanation: The words in sentence are ["Leetcode", "is", "cool"]. |
| 36 | +- Leetcode's last character is not equal to is's first character. |
| 37 | +The sentence is not circular. |
| 38 | +
|
| 39 | +Constraints: |
| 40 | +* 1 <= sentence.length <= 500 |
| 41 | +* sentence consist of only lowercase and uppercase English letters and spaces. |
| 42 | +* The words in sentence are separated by a single space. |
| 43 | +* There are no leading or trailing spaces. |
| 44 | +=end |
| 45 | + |
| 46 | +# @param {String} sentence |
| 47 | +# @return {Boolean} |
| 48 | +def is_circular_sentence(sentence) |
| 49 | + n = sentence.length |
| 50 | + |
| 51 | + return false if sentence[0] != sentence[n - 1] |
| 52 | + |
| 53 | + (1..n - 2).each do |i| |
| 54 | + if sentence[i] == " " |
| 55 | + return false if sentence[i - 1] != sentence[i + 1] |
| 56 | + end |
| 57 | + end |
| 58 | + |
| 59 | + true |
| 60 | +end |
| 61 | + |
| 62 | +# **************** # |
| 63 | +# TEST # |
| 64 | +# **************** # |
| 65 | + |
| 66 | +require "test/unit" |
| 67 | +class Test_is_circular_sentence < Test::Unit::TestCase |
| 68 | + def test_ |
| 69 | + assert_equal true, is_circular_sentence("leetcode exercises sound delightful") |
| 70 | + assert_equal true, is_circular_sentence("eetcode") |
| 71 | + assert_equal false, is_circular_sentence("Leetcode is cool") |
| 72 | + end |
| 73 | +end |
0 commit comments