• + 0 comments

    python solution

    def matchingStrings(stringList, queries):
        # Write your code here
    
        sortedList = sorted(stringList)
        ans = []
        idx = -1
        matchCount = 0
        for q in queries:
            idx = binary_search_bisect(sortedList, q)
    
            if idx == -1:
                ans.append(matchCount)
                continue
    
            for i in range(idx, len(sortedList)):
                if sortedList[i] != q:
                    break
                else:
                    matchCount = matchCount + 1
    
            ans.append(matchCount)
            matchCount = 0
    
        return ans
    
    def binary_search_bisect(sorted_list, target):
        i = bisect.bisect_left(sorted_list, target)
        if i != len(sorted_list) and sorted_list[i] == target:
            return i
        return -1