Problem · Array

Character Frequencies Across Nested String Lists

Learn this problem
EasySnowflakeFULLTIMEPHONE SCREEN
See Snowflake hiring insights

Problem statement

Given a two-level array of string groups, count the total number of occurrences of every character across every string.

Traverse the outer array from left to right, each inner array from left to right, and each string from left to right. Return the frequency map as a two-column String[][]. Each row must be [character, count], where character is a one-character string and count is its decimal frequency. Order rows by the first appearance of each character during that traversal.

Characters are case-sensitive, and spaces and punctuation are counted like any other printable ASCII character. Empty inner arrays and empty strings are allowed. If no character appears, return an empty matrix. Do not use a built-in frequency-counter utility such as Python's Counter.

Function

nestedCharacterFrequencies(groups: String[][]) → String[][]

Examples

Example 1

groups = [["hello"],["world"]]return = [["h","1"],["e","1"],["l","3"],["o","2"],["w","1"],["r","1"],["d","1"]]

The traversal first visits "hello" and then "world". It first discovers h, e, l, o, w, r, d; l occurs three times, o occurs twice, and every other character occurs once.

Example 2

groups = [["Aa",""],[],["!A"," a"]]return = [["A","2"],["a","2"],["!","1"],[" ","1"]]

The empty string and empty inner array contribute no characters. Uppercase A and lowercase a are distinct, and the exclamation mark and space are counted in first-appearance order.

Example 3

groups = [[],[""],[]]return = []

No string contains a character, so the frequency matrix is empty.

Constraints

  • 0 <= groups.length <= 10^3
  • 0 <= groups[i].length <= 10^3
  • Across all inner arrays, there are at most 10^4 strings.
  • Across all strings, there are at most 2 * 10^5 characters.
  • Every string contains only printable ASCII characters.

More Snowflake problems

drafts saved locally
public String[][] nestedCharacterFrequencies(String[][] groups) {
  // write your code here
}
groups[["hello"],["world"]]
expected[["h", "1", "e", "1", "l", "3", "o", "2", "w", "1", "r", "1", "d", "1"]]
checking account