Skip to content

Commit

Permalink
amazon review score
Browse files Browse the repository at this point in the history
  • Loading branch information
brendonmiranda committed Oct 9, 2024
1 parent 26912ec commit 0a9b81e
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
82 changes: 82 additions & 0 deletions src/main/java/aws/AmazonReviewScore.java
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;
}


}
17 changes: 17 additions & 0 deletions src/test/java/aws/AmazonReviewScoreTest.java
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"}));
}

}

0 comments on commit 0a9b81e

Please sign in to comment.