Skip to content

Commit

Permalink
Create 1813. Sentence Similarity III (#606)
Browse files Browse the repository at this point in the history
  • Loading branch information
Chayandas07 authored Oct 6, 2024
2 parents e0e5cf9 + 115007b commit 3032092
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions 1813. Sentence Similarity III
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
class Solution {
public:
bool areSentencesSimilar(string sentence1, string sentence2) {
if(sentence1.length()>sentence2.length()){
swap(sentence1,sentence2);
}
// cout<<sentence1<<" "<<sentence2;
vector<string>v1,v2;
string temp="";
for(int i=0;i<sentence1.length();i++){
if(sentence1[i]==' '){
v1.push_back(temp);
temp="";
}
else{
temp+=sentence1[i];
}
}
v1.push_back(temp);

temp="";
for(int i=0;i<sentence2.length();i++){
if(sentence2[i]==' '){
v2.push_back(temp);
temp="";
}
else{
temp+=sentence2[i];
}
}
v2.push_back(temp);
int i=0;
while(i<v1.size() && v1[i]==v2[i]){
i++;
}
int j=v1.size()-1;
int k=v2.size()-1;
while(j>=0 && v1[j]==v2[k]){
j--;
k--;
}
if(i==v1.size()){
return true;
}
if(j==-1){
return true;
}
// cout<<i<<" "<<j;
if(i>=j+1){
return true;
}
return false;
}
};

0 comments on commit 3032092

Please sign in to comment.