-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy path2490-circular-sentence.py
More file actions
18 lines (16 loc) · 753 Bytes
/
2490-circular-sentence.py
File metadata and controls
18 lines (16 loc) · 753 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# time complexity: O(n)
# space complexity: O(n)
class Solution:
def isCircularSentence(self, sentence: str) -> bool:
stack = []
for word in sentence.split(" "):
if stack and stack[-1] == word[0]:
stack.pop()
else:
stack.append(word[0])
stack.append(word[-1])
return stack[0] == stack[1] and len(stack) == 2
sentence = "JuFZhkkASkRxIeiCOdGeELCMYHmuiqqLQqtjSxxHqnKrQOIEMxzkbapCCTvwaBlLXcGBVjqvppTJCttpbgbEcPEiSAcfcapXEUCdoQJvaUPCHRqVPuOghLqHWbFYgcMvxIufQTjpzmSMCAusXISLbJ aXOmiAAoYDyqqXwAe VnWA WZg uKlnD L UHRD"
print(Solution().isCircularSentence(sentence))
sentence = "leetcode exercises sound delightful"
print(Solution().isCircularSentence(sentence))