Skip to content

Commit efb0682

Browse files
committed
Add solution #2490
1 parent 48dc52e commit efb0682

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@
309309
2427|[Number of Common Factors](./2427-number-of-common-factors.js)|Easy|
310310
2469|[Convert the Temperature](./2469-convert-the-temperature.js)|Easy|
311311
2482|[Difference Between Ones and Zeros in Row and Column](./2482-difference-between-ones-and-zeros-in-row-and-column.js)|Medium|
312+
2490|[Circular Sentence](./2490-circular-sentence.js)|Easy|
312313

313314
## License
314315

solutions/2490-circular-sentence.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* 2490. Circular Sentence
3+
* https://leetcode.com/problems/circular-sentence/
4+
* Difficulty: Easy
5+
*
6+
* A sentence is a list of words that are separated by a single space with no leading or trailing spaces.
7+
*
8+
* For example, "Hello World", "HELLO", "hello world hello world" are all sentences.
9+
* Words consist of only uppercase and lowercase English letters. Uppercase and lowercase English letters
10+
* are considered different.
11+
*
12+
* A sentence is circular if:
13+
* - The last character of a word is equal to the first character of the next word.
14+
* - The last character of the last word is equal to the first character of the first word.
15+
*
16+
* For example, "leetcode exercises sound delightful", "eetcode", "leetcode eats soul" are all circular
17+
* sentences. However, "Leetcode is cool", "happy Leetcode", "Leetcode" and "I like Leetcode" are not
18+
* circular sentences.
19+
*
20+
* Given a string sentence, return true if it is circular. Otherwise, return false.
21+
*/
22+
23+
/**
24+
* @param {string} sentence
25+
* @return {boolean}
26+
*/
27+
var isCircularSentence = function(sentence) {
28+
const match = sentence.match(/^\w$|^(\w).*\1$/) !== null;
29+
const count = sentence.match(/(\w)(?=\s\1)/g)?.length || 0;
30+
31+
return match && count === sentence.split(/\s/).length - 1;
32+
};

0 commit comments

Comments
 (0)