File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public boolean areSentencesSimilar (String sentence1 , String sentence2 ) {
3
+ return isSimilar (sentence1 .split (" " ), sentence2 .split (" " )) || isSimilar (sentence2 .split (" " ),
4
+ sentence1 .split (" " ));
5
+ }
6
+
7
+ private boolean isSimilar (String [] matcher , String [] target ) {
8
+ int targetStartIdx = 0 ;
9
+ int matcherCurrentIdx = 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
+ return true ;
19
+ }
20
+ int targetEndIdx = target .length - 1 ;
21
+ matcherCurrentIdx = matcher .length - 1 ;
22
+ while (targetEndIdx >= targetStartIdx && matcherCurrentIdx >= 0 ) {
23
+ if (!matcher [matcherCurrentIdx ].equals (target [targetEndIdx ])) {
24
+ return false ;
25
+ }
26
+ targetEndIdx --;
27
+ matcherCurrentIdx --;
28
+ }
29
+ return targetEndIdx == targetStartIdx - 1 ;
30
+ }
31
+ }
You can’t perform that action at this time.
0 commit comments