mediumHashingPattern: Hashing

Frequency Counter of String Patterns Solution

Problem Statement

Given a list of strings `patterns`, return a map where keys are the unique patterns and values are their respective frequencies. The order of patterns in the output map does not matter.

Examples

Example 1:
Input:["abc", "def", "abc", "ghi", "def", "abc"]
Output:{"abc":3,"def":2,"ghi":1}
Explanation: The pattern 'abc' appears 3 times, 'def' appears 2 times, and 'ghi' appears 1 time. All other patterns appear 0 times.
Example 2:
Input:["a", "b", "a", "a", "c"]
Output:{"a":3,"b":1,"c":1}
Explanation: The pattern 'a' appears 3 times, 'b' appears 1 time, and 'c' appears 1 time.
Example 3:
Input:[]
Output:{}
Explanation: An empty input list results in an empty frequency map.

Constraints

  • The number of patterns in the list can range from 0 to 10^5.
  • Each pattern string can have a length from 0 to 50.
  • Patterns consist of lowercase English letters.
  • The total length of all strings in the list will not exceed 10^6.
Time: O(N*L) Space: O(U*L)
Use a hash map (dictionary/object) to store frequencies. Iterate through the input list once. For each pattern, if it's in the map, increment its count; otherwise, add it to the map with a count of 1. This processes each pattern in roughly constant time on average.

Run, Test & Submit Code

Ready to practice this challenge? Launch our interactive compilation environment with compiler validation.

Solve on Interactive Workspace

Tested Solutions

from collections import Counter def frequency_counter_of_string_patterns(patterns): return dict(Counter(patterns))