import heapq
from collections import Counter


def topkfrequentII(keywords, reviews, k):
    elem = {}
    for review in reviews:
        for key in keywords:
            if key.lower() in review.lower():
                elem[key] = elem.get(key, 0)+1

    heap = []
    for key, freq in elem.items():
        heap.append((-freq, key))

    heapq.heapify(heap)
    return [heapq.heappop(heap)[1] for _ in range(k)]


def topKFrequent(keywords, reviews, k):

    heap = []
    totalWords = []

    for review in reviews:
        words = set(review.lower().split())
        totalWords.extend(words)

    wordFreq = Counter(totalWords)
    for word, freq in wordFreq.items():
        if word in keywords:
            heap.append((-freq, word))

    heapq.heapify(heap)
    return [heapq.heappop(heap)[1] for _ in range(k)]

    # print(totalWords)


k = 2
keywords = ["anacell", "cetracular", "betacellular"]
reviews = [
    "Anacell provides the best services in the city",
    "betacellular has awesome services",
    "Best services provided by anacell, everyone should use anacell",
]

# Output:
# ["anacell", "betacellular"]

# Explanation:
# "anacell" is occuring in 2 different reviews and "betacellular" is only occuring in 1 review.

print(topKFrequent(keywords, reviews, k))

afb8f129083d3b3c0ab642b7dfa3eee6546c8f96
<mask token>


def topkfrequentII(keywords, reviews, k):
    elem = {}
    for review in reviews:
        for key in keywords:
            if key.lower() in review.lower():
                elem[key] = elem.get(key, 0) + 1
    heap = []
    for key, freq in elem.items():
        heap.append((-freq, key))
    heapq.heapify(heap)
    return [heapq.heappop(heap)[1] for _ in range(k)]


def topKFrequent(keywords, reviews, k):
    heap = []
    totalWords = []
    for review in reviews:
        words = set(review.lower().split())
        totalWords.extend(words)
    wordFreq = Counter(totalWords)
    for word, freq in wordFreq.items():
        if word in keywords:
            heap.append((-freq, word))
    heapq.heapify(heap)
    return [heapq.heappop(heap)[1] for _ in range(k)]


<mask token>

<mask token>


def topkfrequentII(keywords, reviews, k):
    elem = {}
    for review in reviews:
        for key in keywords:
            if key.lower() in review.lower():
                elem[key] = elem.get(key, 0) + 1
    heap = []
    for key, freq in elem.items():
        heap.append((-freq, key))
    heapq.heapify(heap)
    return [heapq.heappop(heap)[1] for _ in range(k)]


def topKFrequent(keywords, reviews, k):
    heap = []
    totalWords = []
    for review in reviews:
        words = set(review.lower().split())
        totalWords.extend(words)
    wordFreq = Counter(totalWords)
    for word, freq in wordFreq.items():
        if word in keywords:
            heap.append((-freq, word))
    heapq.heapify(heap)
    return [heapq.heappop(heap)[1] for _ in range(k)]


<mask token>
print(topKFrequent(keywords, reviews, k))

<mask token>


def topkfrequentII(keywords, reviews, k):
    elem = {}
    for review in reviews:
        for key in keywords:
            if key.lower() in review.lower():
                elem[key] = elem.get(key, 0) + 1
    heap = []
    for key, freq in elem.items():
        heap.append((-freq, key))
    heapq.heapify(heap)
    return [heapq.heappop(heap)[1] for _ in range(k)]


def topKFrequent(keywords, reviews, k):
    heap = []
    totalWords = []
    for review in reviews:
        words = set(review.lower().split())
        totalWords.extend(words)
    wordFreq = Counter(totalWords)
    for word, freq in wordFreq.items():
        if word in keywords:
            heap.append((-freq, word))
    heapq.heapify(heap)
    return [heapq.heappop(heap)[1] for _ in range(k)]


k = 2
keywords = ['anacell', 'cetracular', 'betacellular']
reviews = ['Anacell provides the best services in the city',
    'betacellular has awesome services',
    'Best services provided by anacell, everyone should use anacell']
print(topKFrequent(keywords, reviews, k))

import heapq
from collections import Counter


def topkfrequentII(keywords, reviews, k):
    elem = {}
    for review in reviews:
        for key in keywords:
            if key.lower() in review.lower():
                elem[key] = elem.get(key, 0) + 1
    heap = []
    for key, freq in elem.items():
        heap.append((-freq, key))
    heapq.heapify(heap)
    return [heapq.heappop(heap)[1] for _ in range(k)]


def topKFrequent(keywords, reviews, k):
    heap = []
    totalWords = []
    for review in reviews:
        words = set(review.lower().split())
        totalWords.extend(words)
    wordFreq = Counter(totalWords)
    for word, freq in wordFreq.items():
        if word in keywords:
            heap.append((-freq, word))
    heapq.heapify(heap)
    return [heapq.heappop(heap)[1] for _ in range(k)]


k = 2
keywords = ['anacell', 'cetracular', 'betacellular']
reviews = ['Anacell provides the best services in the city',
    'betacellular has awesome services',
    'Best services provided by anacell, everyone should use anacell']
print(topKFrequent(keywords, reviews, k))

import heapq
from collections import Counter


def topkfrequentII(keywords, reviews, k):
    elem = {}
    for review in reviews:
        for key in keywords:
            if key.lower() in review.lower():
                elem[key] = elem.get(key, 0)+1

    heap = []
    for key, freq in elem.items():
        heap.append((-freq, key))

    heapq.heapify(heap)
    return [heapq.heappop(heap)[1] for _ in range(k)]


def topKFrequent(keywords, reviews, k):

    heap = []
    totalWords = []

    for review in reviews:
        words = set(review.lower().split())
        totalWords.extend(words)

    wordFreq = Counter(totalWords)
    for word, freq in wordFreq.items():
        if word in keywords:
            heap.append((-freq, word))

    heapq.heapify(heap)
    return [heapq.heappop(heap)[1] for _ in range(k)]

    # print(totalWords)


k = 2
keywords = ["anacell", "cetracular", "betacellular"]
reviews = [
    "Anacell provides the best services in the city",
    "betacellular has awesome services",
    "Best services provided by anacell, everyone should use anacell",
]

# Output:
# ["anacell", "betacellular"]

# Explanation:
# "anacell" is occuring in 2 different reviews and "betacellular" is only occuring in 1 review.

print(topKFrequent(keywords, reviews, k))
