|
| 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