Christmas string

3.9

14 votes
Easy-Medium, Approved, Greedy algorithm
Problem

Little Santa has made a list of all the nice children to whom he have to give the present. While getting ready to deliver the gifts he forgot where he kept the list. He remembered a string S containing lowercase letters and stars ( * ). Star denotes that he didn't remember that letter of the string S.

enter image description here

Little Santa is wondering what is the maximum number of names of the nice children in the list that can possibly match with the string S if he will change at most one letter in the string S to a star. String A can possibly match with the string S if letters on non star positions are equal. He is getting late to deliver the gifts so he asked you to solve this.

Note: All the names and the string S will have same length.

Input format:
First line contains a string S (1|S|3000), denoting the star string. S only contains lowercase letters and stars ( * ). Second line contains an integer N (1N3000), denoting the number of names on the list. Next N lines contains a string each, stri (1|stri|3000), denoting the names of the children on the list. stri consists of only lowercase letters.

Output format:
Print an integer denoting the maximum number of names of the nice children in the list that can possibly match with the string S if he will change at most one letter in the string S to a star.

Sample Input
a*c
3
abc
add
agd
Sample Output
3
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

We can substitute third character with * , as a result we'll get a**. All 3 strings match this one.

Editor Image

?