Skip to content

Commit 1ab937e

Browse files
committed
New Problem Solution - "1813. Sentence Similarity III"
1 parent 8ac73b4 commit 1ab937e

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ LeetCode
1313
|1818|[Minimum Absolute Sum Difference](https://leetcode.com/problems/minimum-absolute-sum-difference/) | [C++](./algorithms/cpp/minimumAbsoluteSumDifference/MinimumAbsoluteSumDifference.cpp)|Medium|
1414
|1817|[Finding the Users Active Minutes](https://leetcode.com/problems/finding-the-users-active-minutes/) | [C++](./algorithms/cpp/findingTheUsersActiveMinutes/FindingTheUsersActiveMinutes.cpp)|Medium|
1515
|1816|[Truncate Sentence](https://leetcode.com/problems/truncate-sentence/) | [C++](./algorithms/cpp/truncateSentence/TruncateSentence.cpp)|Easy|
16+
|1813|[Sentence Similarity III](https://leetcode.com/problems/sentence-similarity-iii/) | [C++](./algorithms/cpp/sentenceSimilarity/SentenceSimilarity.III.cpp)|Medium|
1617
|1812|[Determine Color of a Chessboard Square](https://leetcode.com/problems/determine-color-of-a-chessboard-square/) | [C++](./algorithms/cpp/determineColorOfAChessboardSquare/DetermineColorOfAChessboardSquare.cpp)|Easy|
1718
|1808|[Maximize Number of Nice Divisors](https://leetcode.com/problems/maximize-number-of-nice-divisors/) | [C++](./algorithms/cpp/maximizeNumberOfNiceDivisors/MaximizeNumberOfNiceDivisors.cpp)|Hard|
1819
|1807|[Evaluate the Bracket Pairs of a String](https://leetcode.com/problems/evaluate-the-bracket-pairs-of-a-string/) | [C++](./algorithms/cpp/evaluateTheBracketPairsOfAString/EvaluateTheBracketPairsOfAString.cpp)|Medium|
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// Source : https://leetcode.com/problems/sentence-similarity-iii/
2+
// Author : Hao Chen
3+
// Date : 2021-04-06
4+
5+
/*****************************************************************************************************
6+
*
7+
* A sentence is a list of words that are separated by a single space with no leading or trailing
8+
* spaces. For example, "Hello World", "HELLO", "hello world hello world" are all sentences. Words
9+
* consist of only uppercase and lowercase English letters.
10+
*
11+
* Two sentences sentence1 and sentence2 are similar if it is possible to insert an arbitrary sentence
12+
* (possibly empty) inside one of these sentences such that the two sentences become equal. For
13+
* example, sentence1 = "Hello my name is Jane" and sentence2 = "Hello Jane" can be made equal by
14+
* inserting "my name is" between "Hello" and "Jane" in sentence2.
15+
*
16+
* Given two sentences sentence1 and sentence2, return true if sentence1 and sentence2 are similar.
17+
* Otherwise, return false.
18+
*
19+
* Example 1:
20+
*
21+
* Input: sentence1 = "My name is Haley", sentence2 = "My Haley"
22+
* Output: true
23+
* Explanation: sentence2 can be turned to sentence1 by inserting "name is" between "My" and "Haley".
24+
*
25+
* Example 2:
26+
*
27+
* Input: sentence1 = "of", sentence2 = "A lot of words"
28+
* Output: false
29+
* Explanation: No single sentence can be inserted inside one of the sentences to make it equal to the
30+
* other.
31+
*
32+
* Example 3:
33+
*
34+
* Input: sentence1 = "Eating right now", sentence2 = "Eating"
35+
* Output: true
36+
* Explanation: sentence2 can be turned to sentence1 by inserting "right now" at the end of the
37+
* sentence.
38+
*
39+
* Example 4:
40+
*
41+
* Input: sentence1 = "Luky", sentence2 = "Lucccky"
42+
* Output: false
43+
*
44+
* Constraints:
45+
*
46+
* 1 <= sentence1.length, sentence2.length <= 100
47+
* sentence1 and sentence2 consist of lowercase and uppercase English letters and spaces.
48+
* The words in sentence1 and sentence2 are separated by a single space.
49+
******************************************************************************************************/
50+
51+
class Solution {
52+
private:
53+
bool isWord(char ch) {
54+
return (ch >='a' && ch <= 'z' ) || ( ch >='A' && ch <= 'Z');
55+
}
56+
bool isSpace(char ch) {
57+
return (ch == ' ');
58+
}
59+
void split(string& s, vector<string>& words) {
60+
s += ' ';
61+
int start=0, end=0;
62+
while(start < s.size()) {
63+
while(isSpace(s[start])) start++;
64+
end = start;
65+
while(!isSpace(s[end])) end++;
66+
words.push_back(s.substr(start, end-start));
67+
start = end + 1;
68+
}
69+
}
70+
void print(vector<string>& v) {
71+
cout << "[";
72+
for(int i=0; i<v.size()-1; i++) {
73+
cout << v[i] << ", ";
74+
}
75+
cout << v[v.size()-1] << "]" << endl;
76+
}
77+
public:
78+
bool areSentencesSimilar(string sentence1, string sentence2) {
79+
string& longstr = sentence1.size() >= sentence2.size() ? sentence1 : sentence2;
80+
string& shortstr = sentence1.size() < sentence2.size() ? sentence1 : sentence2;
81+
if ( longstr == shortstr ) return true;
82+
83+
vector<string> words1, words2;
84+
split(shortstr, words1);
85+
split(longstr, words2);
86+
//print(words1); print(words2);
87+
88+
int left=0, right=words1.size()-1;
89+
while(left< words1.size() && words1[left] == words2[left]) left++;
90+
91+
int delta = words2.size() - words1.size();
92+
while(right>=left && words1[right] == words2[delta+right]) right--;
93+
//cout << left << ":" << right << ":" << delta << endl;
94+
95+
return left > right;
96+
}
97+
};

0 commit comments

Comments
 (0)