problem
solution
1 | class Solution { |
- one pass
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18class Solution {
public:
bool isCircularSentence(string sentence) {
int n = sentence.size();
int l=0, r = n-1;
if(sentence[l]!=sentence[n-1]) return false;
r = l;
while(r<n && sentence[r]!=' ') r++;
while(r<n){
l = r-1;
r++;
if(sentence[l]!=sentence[r]) return false;
while(r<n && sentence[r]!=' ') r++;
}
return true;
}
};analysis
- time complexity
O(n)
- space complexity
O(n)
O(1)