|
| 1 | +classSolution { |
| 2 | +publicbooleanareSentencesSimilar(Stringsentence1,Stringsentence2) { |
| 3 | +returnisSimilar(sentence1.split(" "),sentence2.split(" ")) ||isSimilar(sentence2.split(" "), |
| 4 | +sentence1.split(" ")); |
| 5 | + } |
| 6 | + |
| 7 | +privatebooleanisSimilar(String[]matcher,String[]target) { |
| 8 | +inttargetStartIdx =0; |
| 9 | +intmatcherCurrentIdx =0; |
| 10 | +while (targetStartIdx <target.length &&matcherCurrentIdx <matcher.length) { |
| 11 | +if (!matcher[matcherCurrentIdx].equals(target[targetStartIdx])) { |
| 12 | +break; |
| 13 | + } |
| 14 | +targetStartIdx++; |
| 15 | +matcherCurrentIdx++; |
| 16 | + } |
| 17 | +if (targetStartIdx ==target.length) { |
| 18 | +returntrue; |
| 19 | + } |
| 20 | +inttargetEndIdx =target.length -1; |
| 21 | +matcherCurrentIdx =matcher.length -1; |
| 22 | +while (targetEndIdx >=targetStartIdx &&matcherCurrentIdx >=0) { |
| 23 | +if (!matcher[matcherCurrentIdx].equals(target[targetEndIdx])) { |
| 24 | +returnfalse; |
| 25 | + } |
| 26 | +targetEndIdx--; |
| 27 | +matcherCurrentIdx--; |
| 28 | + } |
| 29 | +returntargetEndIdx ==targetStartIdx -1; |
| 30 | + } |
| 31 | +} |