File tree Expand file tree Collapse file tree 1 file changed +54
-0
lines changed Expand file tree Collapse file tree 1 file changed +54
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ bool areSentencesSimilar(string sentence1, string sentence2) {
4
+ if(sentence1.length()>sentence2.length()){
5
+ swap(sentence1,sentence2);
6
+ }
7
+ // cout<<sentence1<<" "<<sentence2;
8
+ vector<string>v1,v2;
9
+ string temp="";
10
+ for(int i=0;i<sentence1.length();i++){
11
+ if(sentence1[i]==' '){
12
+ v1.push_back(temp);
13
+ temp="";
14
+ }
15
+ else{
16
+ temp+=sentence1[i];
17
+ }
18
+ }
19
+ v1.push_back(temp);
20
+
21
+ temp="";
22
+ for(int i=0;i<sentence2.length();i++){
23
+ if(sentence2[i]==' '){
24
+ v2.push_back(temp);
25
+ temp="";
26
+ }
27
+ else{
28
+ temp+=sentence2[i];
29
+ }
30
+ }
31
+ v2.push_back(temp);
32
+ int i=0;
33
+ while(i<v1.size() && v1[i]==v2[i]){
34
+ i++;
35
+ }
36
+ int j=v1.size()-1;
37
+ int k=v2.size()-1;
38
+ while(j>=0 && v1[j]==v2[k]){
39
+ j--;
40
+ k--;
41
+ }
42
+ if(i==v1.size()){
43
+ return true;
44
+ }
45
+ if(j==-1){
46
+ return true;
47
+ }
48
+ // cout<<i<<" "<<j;
49
+ if(i>=j+1){
50
+ return true;
51
+ }
52
+ return false;
53
+ }
54
+ };
You can’t perform that action at this time.
0 commit comments