diff options
-rw-r--r-- | circular_sentence.c | 30 | ||||
-rw-r--r-- | circular_sentence.py | 34 |
2 files changed, 64 insertions, 0 deletions
diff --git a/circular_sentence.c b/circular_sentence.c new file mode 100644 index 0000000..93a48e8 --- /dev/null +++ b/circular_sentence.c @@ -0,0 +1,30 @@ +// 2490. Circular Sentence +#include "leetcode.h" + +/* + * a sentence is a list of words that are separated by a single space with no + * leading or trailing spaces. words consist of only uppercase and lowercase + * english letters. uppercase and lowercase english letters are considered + * different. a sentence is circular if the last character of a word is equal to + * the first character of the next word, the last chracter of the last word is + * equal to the first character of the first word. given a string 'sentence', + * return 'true' if it is a circular sentence, and 'false' otherwise. + */ + +bool isCircularSentence(char *sentence) { + int n = strlen(sentence); + if (sentence[0] != sentence[n - 1]) + return false; + for (int i = 0; i < n; i++) + if (sentence[i] == ' ' && sentence[i - 1] != sentence[i + 1]) + return false; + return true; +} + +int main() { + char *s1 = "leetcode exercises sound delightful", *s2 = "eetcode", + *s3 = "Leetcode is cool"; + printf("%d\n", isCircularSentence(s1)); // expect: 1 + printf("%d\n", isCircularSentence(s2)); // expect: 1 + printf("%d\n", isCircularSentence(s3)); // expect: 0 +} diff --git a/circular_sentence.py b/circular_sentence.py new file mode 100644 index 0000000..9615c1d --- /dev/null +++ b/circular_sentence.py @@ -0,0 +1,34 @@ +# 2490. Circular Sentence + +""" +a sentence is a list of words that are separated by a single space with no +leading or trailing spaces. words consist of only uppercase and lowercase +english letters. uppercase and lowercase english letters are considered +different. a sentence is circular if the last character of a word is equal to +the first character of the next word, the last chracter of the last word is +equal to the first character of the first word. given a string 'sentence', +return 'true' if it is a circular sentence, and 'false' otherwise. +""" + + +class Solution(object): + def isCircularSentence(self, sentence): + """ + :type sentence: str + :rtype: bool + """ + n = len(sentence) + if sentence[0] != sentence[n - 1]: + return False + for i in range(1, n - 1): + if sentence[i] == " ": + if sentence[i - 1] != sentence[i + 1]: + return False + return True + + +if __name__ == "__main__": + obj = Solution() + print(obj.isCircularSentence(sentence="leetcode exercises sound delightful")) + print(obj.isCircularSentence(sentence="eetcode")) + print(obj.isCircularSentence(sentence="Leetcode is cool")) |