Count Tag Occurrences
Problem statement
Each string in records contains comma-separated tags. Count every occurrence of each tag across all records, including repeated appearances within one record.
Split on commas, trim ASCII spaces from both ends of each field, and ignore empty fields. Tags are case-sensitive. Return one string tag:count for each distinct tag, ordered lexicographically by tag. Counts use ordinary decimal digits with no spaces. Empty input or records containing only empty fields return an empty array.
Function
countTagOccurrences(records: String[]) → String[]Examples
Example 1
records = ["tag1,tag2,tag1","tag2,tag3"]return = ["tag1:2","tag2:2","tag3:1"]Across both records, tag1 occurs twice, tag2 twice, and tag3 once. Both occurrences of tag1 in the first record count.
Example 2
records = [" Team ,team,,Team",""]return = ["Team:2","team:1"]Trim surrounding spaces and ignore empty fields. Tags are case-sensitive: Team occurs twice and team once. Uppercase T sorts before lowercase t.
Example 3
records = []return = []With no records, there are no tag occurrences to report.
Constraints
0 <= records.length <= 10000 <= records[i].length <= 5000- The total number of characters in all records is at most
5000. - After trimming ASCII spaces, each comma-separated field is empty or contains
1through40characters fromA-Z,a-z,0-9,_, and-.