A sentence is a list of words that are separated by a single space with no leading or trailing spaces.
"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:
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.sentence
are separated by a single space.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 "]
temp.f95:27:29: 27 | if (words(iword)(len_trim(words(iword))) /= words(iword+1)(1)) then | 1 Error: Syntax error in IF-expression at (1) temp.f95:30:15: 30 | end if | 1 Error: Expecting END DO statement at (1) temp.f95:34:26: 34 | if (words(nwords)(len_trim(words(nwords))) /= words(1)(1)) then | 1 Error: Syntax error in IF-expression at (1) temp.f95:37:11: 37 | end if | 1 Error: Expecting END FUNCTION statement at (1) temp.f95:50:17: 50 | allocate(words(nwords)) | 1 Error: Allocate-object at (1) with a deferred type parameter requires either a type-spec or SOURCE tag or a MOLD tag temp.f95:67:63: 67 | character(len=500), parameter :: examples(3) = ["Hello World ", & | 1 Error: Different CHARACTER lengths (12/6) in array constructor at (1) temp.f95:49:23: 49 | nwords = count(str == ' ') + 1 | 1 Error: ‘mask’ argument of ‘count’ intrinsic at (1) must be a logical array temp.f95:8:19: 8 | sentence = examples(i) | 1 Error: Function ‘examples’ at (1) has no IMPLICIT type temp.f95:7:27: 7 | do i = 1, size(examples) | 1 Error: Symbol ‘examples’ at (1) has no IMPLICIT type
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
At line 7 of file temp.f95 (unit = 5, file = 'stdin') Fortran runtime error: End of file Error termination. Backtrace: #0 0x7f7fc945a960 in ??? #1 0x7f7fc945b4d9 in ??? #2 0x7f7fc96af17b in ??? #3 0x7f7fc96a8684 in ??? #4 0x7f7fc96a92aa in ??? #5 0x5bd649cce2e2 in MAIN__ #6 0x5bd649cce426 in main
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
.
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
.