Amazon Online Assessment (OA) 2021 - Most Common Word with Exclusion List | HackerRank SHL

Find the most frequently used word, which is not in the ignored_keywords, within a paragraph. A paragraph is a single line of words that may contain punctuation marks, and mixed with uppercase and lowercase letters. The word comparison should not be case-sensitive, and the output word is expected to be in lowercase.

Examples

Example 1:

Input:

paragraph = "If this book was written today in the midst of the slew of dystopian novels that come out, it may not have stood out. But, this book was way ahead of its time." ignored_keywords = ["of", "was", "the"]

Output: "book"
Explanation:

"of" appears three times and "was", "the" appear twice, but they are in the ignored_keywords list. The next most common word is "book", which appears twice.

Constraints

There is always at least one word in the paragraph and at least one word in the list of excluded keywords. The most common keyword frequency count that is not in ignored_keywords will always be unique. Keywords in the exclusion list consist only of lowercase alphabetical characters.

Try it yourself

Solution

1import re
2from typing import Counter, List
3
4def most_common_word(paragraph: str, banned: List[str]) -> str:
5    ban = set(banned)
6    counts = Counter(
7        word
8        for m in re.finditer(r'\w+', paragraph)
9        for word in [m[0].lower()]
10        if word not in ban
11    )
12    [(word, _)] = counts.most_common(1)
13    return word
14
15if __name__ == '__main__':
16    paragraph = input()
17    banned = input().split()
18    res = most_common_word(paragraph, banned)
19    print(res)
20
1import java.util.Arrays;
2import java.util.HashMap;
3import java.util.HashSet;
4import java.util.List;
5import java.util.Map;
6import java.util.Scanner;
7import java.util.regex.Matcher;
8import java.util.regex.Pattern;
9
10class Solution {
11    public static String mostCommonWord(String paragraph, List<String> banned) {
12        HashSet<String> ban = new HashSet<>(banned);
13        HashMap<String, Integer> counts = new HashMap<>();
14        Matcher m = Pattern.compile("\\w+").matcher(paragraph);
15        while (m.find()) {
16            String word = m.group(0).toLowerCase();
17            if (!ban.contains(word))
18                counts.merge(word, 1, Integer::sum);
19        }
20        String mxWord = null;
21        int mxCount = Integer.MIN_VALUE;
22        for (Map.Entry<String, Integer> e : counts.entrySet()) {
23            if (e.getValue() <= mxCount)
24                continue;
25            mxWord = e.getKey();
26            mxCount = e.getValue();
27        }
28        return mxWord;
29    }
30
31    public static List<String> splitWords(String s) {
32        return s.isEmpty() ? List.of() : Arrays.asList(s.split(" "));
33    }
34
35    public static void main(String[] args) {
36        Scanner scanner = new Scanner(System.in);
37        String paragraph = scanner.nextLine();
38        List<String> banned = splitWords(scanner.nextLine());
39        scanner.close();
40        String res = mostCommonWord(paragraph, banned);
41        System.out.println(res);
42    }
43}
44

Got a question?ย Ask the Teaching Assistantย anything you don't understand.

Still not clear? Ask in the Forum, ย Discordย orย Submitย the part you don't understand to our editors.

โ†
โ†‘TA ๐Ÿ‘จโ€๐Ÿซ