I've just started leetcode and am struggling because I've got code and its verified for 264/266 and idk whats wrong with it.
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.
Heres the code I wrote:
class Solution(object):
def isCircularSentence(self, sentence):
length = len(sentence) -1
if sentence[0] == sentence[length]:
breaks = []
for i in range(length):
if sentence[i] == ' ':
breaks.append(i)
for x in breaks:
if sentence[x - 1] != sentence[x + 1]:
return False
if breaks == [] and sentence[0] == sentence[length]:
return True
else:
return False
And on the sentence, 'a a ba' it returns true instead of false