-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
26912ec
commit 0a9b81e
Showing
2 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package aws; | ||
|
||
public class AmazonReviewScore { | ||
|
||
private class Trie { | ||
Trie[] alpha = new Trie[26]; | ||
boolean leaf; | ||
} | ||
|
||
public int amazonReviewScore(String review, String[] prohibitedWords) { | ||
|
||
final Trie root = new Trie(); | ||
for (String prohibitedWord : prohibitedWords) { | ||
|
||
insert(root, prohibitedWord); | ||
} | ||
|
||
int max = 0; | ||
for (int i = 0; i < review.length(); i++) { | ||
|
||
int count = 0; | ||
Trie child = root; | ||
for (int j = i; j < review.length(); j++) { | ||
|
||
int rep = review.toUpperCase().charAt(j) - 'A'; | ||
if (child.alpha[rep] != null) { | ||
|
||
child = child.alpha[rep]; | ||
|
||
if (child.leaf) { | ||
max = Math.max(count, max); | ||
break; | ||
} else { | ||
count += 1; | ||
} | ||
|
||
} else { | ||
child = root; | ||
if (child.alpha[rep] != null) { | ||
// copy paste logic above | ||
child = child.alpha[rep]; | ||
|
||
if (child.leaf) { | ||
max = Math.max(count, max); | ||
break; | ||
} else { | ||
count += 1; | ||
} | ||
// | ||
} else { | ||
count += 1; | ||
} | ||
} | ||
max = Math.max(count, max); | ||
} | ||
} | ||
|
||
return max; | ||
} | ||
|
||
private Trie insert(Trie root, String key) { | ||
|
||
Trie child = root; | ||
|
||
for (int i = 0; i < key.length(); i++) { | ||
|
||
int l = key.toUpperCase().charAt(i) - 'A'; | ||
|
||
if (child.alpha[l] == null) { | ||
child.alpha[l] = new Trie(); | ||
child = child.alpha[l]; | ||
} else { | ||
child = child.alpha[l]; | ||
} | ||
} | ||
child.leaf = true; | ||
|
||
return root; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package aws; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class AmazonReviewScoreTest { | ||
|
||
@Test | ||
public void test() { | ||
AmazonReviewScore amazonReviewScore = new AmazonReviewScore(); | ||
|
||
Assertions.assertEquals(15, amazonReviewScore.amazonReviewScore("GoodProductButScrapAfterWash", new String[]{"crap", "odpro"})); | ||
Assertions.assertEquals(11, amazonReviewScore.amazonReviewScore("FastDeliveryOkayProduct", new String[]{"eryoka", "yo", "eli"})); | ||
Assertions.assertEquals(20, amazonReviewScore.amazonReviewScore("ExtremeValueForMoney", new String[]{"tuper", "douche"})); | ||
} | ||
|
||
} |