Similar Strings
Time limit1sMemory limit1024 MB
Given N strings of length K, delete the fewest so that every pair of adjacent remaining strings shares a matching character at some position.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Bit manipulation, Hash map, String
- Solved
- No attempts yet
Problem
Two strings and of the same length are called similar if the following holds.
- Among all with , there is at least one such that .
There is an array of strings, each of length . You want to delete zero or more elements from this array without changing the order of the remaining elements, so that every pair of adjacent strings in the remaining array is similar. Find the minimum number of elements you must delete.
Input
The first line contains the integers and , separated by a space.
The next lines each contain one string of length .
Every string consists of lowercase English letters only.
Output
Print the answer on the first line.