Alice has K strings, each consisting only of lowercase letters that she wanted to send to her friend. To avoid anyone else from being able to see them, she encodes every string using the following rule:
However, due to a bug, a certain number of messages (possibly none) were not encoded. You are given a list of N distinct strings containing all the original messages as well as the encoded messages. You do not know how many messages Alice originally started with.
Task
Determine the minimum possible value of K denoting the the number of messages Alice initially started with.
Example
Assumptions
Approach
As "aaa" and "zzz" are encodings of each other, so Alice could have started with either one of them initially and got the other one as a result of encoding it, or she could have started with both and neither of them got encoded. For "pqrs", as its encoded string does not exist in the final array, so it must have been there initially and did not get encoded. Thus, there are three choices:
The minimum number of strings Alice could have started with is 2, therefore the answer is 2.
Function description
Complete the function findMessages provided in the editor. This function takes the following 2 parameters and returns the required answer:
Input format
Note: This is the input format that you must use to provide custom input (available above the Compile and Test button).
Output format
Print a single line containing a single integer representing the minimum possible number of strings Alice could have had initially.
Constraints
1≤N≤105
1≤|s|≤10
Code snippets (also called starter code/boilerplate code)
This question has code snippets for C, CPP, Java, and Python.
Given
Approach
There must be a minimum of 3 strings that Alice could have started with to end up with the given final array. Note that "aaa" and "zzz" are encodings of each other, so at least one of them must have been present in the original array. Similarly, at least one of "hack" and "szxp" must have been present. "abcd" does not have its encoding in the final array so it is guaranteed to be present in the original array and did not get encoded.
Here are all the 9 ways that Alice could have ended up with the final array:
The minimum number of strings Alice could have started with is 3, so the answer is 3.