aboutsummaryrefslogtreecommitdiff
path: root/circular_sentence.c
blob: 93a48e8766a6e1f99f92a64b8e993b8008e9f4f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
}