-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdaily138.cpp
More file actions
53 lines (44 loc) · 1.23 KB
/
Copy pathdaily138.cpp
File metadata and controls
53 lines (44 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Solution 1
class Solution {
public:
bool isCircularSentence(string sentence) {
auto words = std::vector<std::string>{};
auto w = std::string{};
for (auto i = 0; i < sentence.size(); ++i) {
if (sentence[i] == ' ') {
words.push_back(w);
w.clear();
} else
w += sentence[i];
}
if (!w.empty())
words.push_back(w);
if (words.size() == 1) {
if (words[0][0] != words[0].back())
return false;
return true;
}
if (words[0][0] - words.back().back() != 0)
return false;
for (auto i = 1; i < words.size(); ++i) {
if (words[i][0] - words[i - 1].back() != 0)
return false;
}
return true;
}
};
// Solution 2
class Solution {
public:
bool isCircularSentence(string sentence) {
if(sentence[0] != sentence[sentence.size() -1]){
return false;
}
for(int i = 1; i<sentence.size()-1; i++){
if(sentence[i] == ' ' and sentence[i-1] != sentence[i+1]){
return false;
}
}
return true;
}
};