tinycodinglm / cp2 /cp-0034.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 15)
b27bfb3 verified
|
Raw History Blame Contribute Delete
3.92 kB
cp-practice-sim trace
System You are a competitive-programming assistant. Deliver correct, complete, runnable solutions that read standard input and write standard output. Give only a concise rationale: approach, correctness idea, and time/space com...

You are a competitive-programming assistant. Deliver correct, complete, runnable solutions that read standard input and write standard output. Give only a concise rationale: approach, correctness idea, and time/space complexity.

User 2026-11-08T07:20:03.537Z

Scrambled Word Families

Topic: Hashing and counting (maps/sets) | Difficulty: easy

Problem

Two words belong to the same family if one can be rearranged into the other (they are anagrams: same letters with same multiplicities). Given n words, find how many different families occur and the size of the largest family.

Input

The first line contains n. Each of the next n lines contains one word of lowercase letters.

Output

Print two integers separated by a space: the number of families and the size of the largest family.

Constraints

  • 1 <= n <= 10^5
  • Each word has length at least 1; the total length of all words is at most 2*10^5

Examples

Example 1

Input:

markdown
6
listen
silent
enlist
google
gogole
cat

Output:

markdown
3 3

Example 2

Input:

markdown
3
ab
ab
ba

Output:

markdown
1 3

Note: Identical words count separately and belong to the same family.

Solve this in Rust, TypeScript, C++17, one complete program per language. Include a concise rationale (approach, correctness idea, time/space complexity).

Assistant 2026-11-08T07:20:05.511Z

Approach. Sort the letters of every word to get a canonical key and count keys in a hash map. The map size is the number of families and the maximum value is the largest family.

Correctness. Two words are anagrams exactly when their sorted letter sequences are equal, so keys partition the words into families.

Complexity. Time: O(L log L) for total length L. Space: O(L).

Rust

rust
use std::collections::HashMap;
use std::io::*;
fn main() {
    let mut t = String::new();
    stdin().read_to_string(&mut t).unwrap();
    let mut it = t.split_whitespace();
    let n: usize = it.next().unwrap().parse().unwrap();
    let mut m: HashMap<Vec<u8>, usize> = HashMap::new();
    let mut best = 0;
    for _ in 0..n {
        let mut w = it.next().unwrap().as_bytes().to_vec();
        w.sort();
        let c = m.entry(w).or_insert(0);
        *c += 1;
        best = best.max(*c);
    }
    println!("{} {}", m.len(), best);
}

TypeScript

typescript
declare const require: any;
const tok: string[] = require('fs').readFileSync(0, 'utf8').split(/\s+/).filter((s: string) => s.length > 0);
const n: number = parseInt(tok[0]);
const m = new Map<string, number>();
let best = 0;
for (let i = 1; i <= n; i++) {
  const key = tok[i].split('').sort().join('');
  const c = (m.get(key) || 0) + 1;
  m.set(key, c);
  if (c > best) best = c;
}
console.log(m.size + " " + best);

C++17

cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
    int n;
    cin >> n;
    unordered_map<string, int> m;
    int best = 0;
    for (int i = 0; i < n; i++) {
        string s;
        cin >> s;
        sort(s.begin(), s.end());
        best = max(best, ++m[s]);
    }
    cout << m.size() << " " << best << endl;
}