forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsentence-similarity-iii.cpp
More file actions
26 lines (25 loc) · 893 Bytes
/
sentence-similarity-iii.cpp
File metadata and controls
26 lines (25 loc) · 893 Bytes
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
// Time: O(n)
// Space: O(1)
class Solution {
public:
bool areSentencesSimilar(string sentence1, string sentence2) {
if (size(sentence1) > size(sentence2)) {
swap(sentence1, sentence2);
}
int count = 0;
for (int step = 0; step < 2; ++step) {
for (int i = 0; i <= size(sentence1); ++i) {
char c1 = i != size(sentence1) ? sentence1[step == 0 ? i : size(sentence1) - 1 - i] : ' ';
char c2 = i != size(sentence2) ? sentence2[step == 0 ? i : size(sentence2) - 1 - i] : ' ';
if (c1 != c2) {
break;
}
if (c1 == ' ') {
++count;
}
}
}
return count >= count_if(cbegin(sentence1), cend(sentence1),
[](char x) { return x == ' '; }) + 1;
}
};