Circular Sentence

🏠 ⬅️ ➡️

A sentence is a list of words that are separated by a single space with no leading or trailing spaces.

  • For example, "Hello World ", "HELLO ", "hello world hello world " are all sentences.

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 character of the last word is equal to the first character of the first word.

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.

Given a string sentence, return true if it is circular. Otherwise, return false.

Example 1:

Input: sentence = "leetcode exercises sound delightful " Output: true Explanation: The words in sentence are [ "leetcode ", "exercises ", "sound ", "delightful "]. - leetcode's last character is equal to exercises's first character. - exercises's last character is equal to sound's first character. - sound's last character is equal to delightful's first character. - delightful's last character is equal to leetcode's first character. The sentence is circular.

Example 2:

Input: sentence = "eetcode " Output: true Explanation: The words in sentence are [ "eetcode "]. - eetcode's last character is equal to eetcode's first character. The sentence is circular.

Example 3:

Input: sentence = "Leetcode is cool " Output: false Explanation: The words in sentence are [ "Leetcode ", "is ", "cool "]. - Leetcode's last character is not equal to is's first character. The sentence is not circular.

Constraints:

  • 1 <= sentence.length <= 500
  • sentence consist of only lowercase and uppercase English letters and spaces.
  • The words in sentence are separated by a single space.
  • There are no leading or trailing spaces.

Note: This problem is from LeetCode.
Compiled
Executed
Correct
program main
    implicit none

    integer :: i
    character(len=500) :: sentence

    do i = 1, size(examples)
        sentence = examples(i)
        write(*,*) 'Sentence: ', trim(sentence), ' Circular: ', is_circular(sentence)
    end do

contains

    function is_circular(sentence) result(circular)
        implicit none
        character(len=*), intent(in) :: sentence
        logical :: circular
        integer :: nwords, iword
        character(len=:), allocatable :: words(:)

        ! Split the sentence into individual words
        call split_string(sentence, words)
        nwords = size(words)

        ! Check if the last character of each word is equal to the first character of the next word
        do iword = 1, nwords - 1
            if (words(iword)(len_trim(words(iword))) /= words(iword+1)(1)) then
                circular = .false.
                return
            end if
        end do

        ! Check if the last character of the last word is equal to the first character of the first word
        if (words(nwords)(len_trim(words(nwords))) /= words(1)(1)) then
            circular = .false.
            return
        end if

        circular = .true.

    end function is_circular

    subroutine split_string(str, words)
        implicit none
        character(len=*), intent(in) :: str
        character(len=:), allocatable, intent(out) :: words(:)
        integer :: pos, nwords

        nwords = count(str == ' ') + 1
        allocate(words(nwords))

        pos = 1
        words(1) = ''
        do while (pos < len(str))
            if (str(pos:pos) == ' ') then
                pos = pos + 1
                cycle
            end if
            words(1) = trim(words(1)) // str(pos:pos)
            pos = pos + 1
        end do

    end subroutine split_string

end program main

character(len=500), parameter :: examples(3) = ["Hello World ", &
                                                 "HELLO ", &
                                                 "hello world hello world "]
Compiled
Executed
Correct
program circular_sentence
      implicit none

      character(len=500) :: sentence
      logical :: is_circular

      read(*,*) sentence

      is_circular = check_circularity(sentence)

      if (is_circular) then
        print *, "The sentence is circular."
      else
        print *, "The sentence is not circular."
      end if

      contains

      function check_circularity(sentence) result(is_circular)
      implicit none

      character(len=*), intent(in) :: sentence
      logical :: is_circular
      integer :: i, j

      is_circular = .false.

      do i = 1, len_trim(sentence) - 1
        j = i + 1
        if (sentence(i:i) == sentence(j:j)) then
          is_circular = .true.
          exit
        end if
      end do

      if (is_circular) then
        if (sentence(i:i) == sentence(1:1)) then
          is_circular = .true.
        end if
      end if

      end function check_circularity
      end program circular_sentence
🌐 Data from online sources
def is_circular(sentence: str) -> bool:
    words = sentence.split()
    for i in range(1, len(words)):
        if words[i - 1][-1] != words[i][0]:
            return False
    return words[-1][-1] == words[0][0]

The algorithm is straightforward: 1. We split the input sentence into individual strings in an array by using the space character as a delimiter. 2. We iterate through the words in the array, checking if the last character of the previous word is equal to the first character of the current word. If not, return false. 3. After the iteration, we check if the last character of the last word in the array is equal to the first character of the first word. If so, we return true; otherwise, return false.

🌐 Data from online sources
bool isCircular(std::string sentence) {
    std::stringstream ss(sentence);
    std::string word, prev;

    while (ss >> word) {
        if (!prev.empty() && prev.back() != word.front()) {
            return false;
        }
        prev = word;
    }

    return word.back() == sentence.front();
}

The algorithm is straightforward: 1. We split the input sentence into individual strings in an array by using the space character as a delimiter. 2. We iterate through the words in the array, checking if the last character of the previous word is equal to the first character of the current word. If not, return false. 3. After the iteration, we check if the last character of the last word in the array is equal to the first character of the first word. If so, we return true; otherwise, return false.