Skip to content

Commit 58c3d32

Browse files
authored
2490. Circular Sentence (#193)
* 2490. Circular Sentence * Fix: remove 1957
1 parent aae8521 commit 58c3d32

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,7 @@
495495
| 2466 | Count Ways To Build Good Strings | [Ruby](./algorithms/ruby/2466-count-ways-to-build-good-strings.rb) | Medium |
496496
| 2477 | Minimum Fuel Cost to Report to the Capital | [Ruby](./algorithms/ruby/2477-minimum-fuel-cost-to-report-to-the-capital.rb) | Medium |
497497
| 2483 | Minimum Penalty for a Shop | [Ruby](./algorithms/ruby/2483-minimum-penalty-for-a-shop.rb) | Medium |
498+
| 2490 | Circular Sentence | [Ruby](./algorithms/ruby/2490-circular-sentence.rb) | Easy |
498499
| 2492 | Minimum Score of a Path Between Two Cities | [Ruby](./algorithms/ruby/2492-minimum-score-of-a-path-between-two-cities.rb) | Medium |
499500
| 2501 | Longest Square Streak in an Array | [Ruby](./algorithms/ruby/2501-longest-square-streak-in-an-array.rb) | Medium |
500501
| 2542 | Maximum Subsequence Score | [Ruby](./algorithms/ruby/2542-maximum-subsequence-score.rb) | Medium |
+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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

Comments
 (0)