Good strings

3.3

7 votes
Combinatorics, Dynamic Programming, Algorithms, String, Introduction to Dynamic Programming 1
Problem

There are N binary strings (consisting of 0s and 1s) in a table. A pair of strings is good if there exists at least one character in common in both strings.
Find the number of good pairs of strings.

Input format

  • The first line contains an integer T denoting the number of test cases.
  • The first line of each test case contains an integer N denoting the number of strings on the table.
  • Next N lines of each test case contain the strings present on the table.

Output format

Print T lines. For each test case:

  • Print a single line indicating the number of good pairs of strings.

Constraints

1T10000

1N100000

1Length of String5

The sum of length over all test cases does not exceed 500000.

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

First Test Case:   String 2 and 3 have character 0 in common

Second test case: All strings have 1 in common so answer is number of pairs, which is 10

Editor Image

?