Name Day
InterviewTime limit1sMemory limit1024 MB
Given a name and a list of the year's name days, find the earliest day reachable by replacing exactly one letter of the name.
- Level
Easy3 of 10
- Topics
- String, Brute force, Implementation, Hash map
- Solved
- No attempts yet
Problem
Your friend likes money. When your friend has a name day, they get money from their generous aunt, so your friend really enjoys it when they have a name day.
Today happens to be your friend's name day, and your friend is wondering how to avoid waiting another whole year before the next name day. You have been asked to help.
Your friend plans to replace exactly one letter in their name so that the next name day comes as soon as possible. Given a list of name days for the coming year, help your friend figure out how soon the next name day can be. Adding or removing characters is not allowed; you may only replace exactly one character with exactly one other character.
If it is impossible to find a name reachable with the allowed operations above, your friend will simply have to wait a whole year.
Input
The input starts with a single string, your friend's name. Then follows an integer on one line, the number of days in the coming year. After that follow strings, the name days for the days of the coming year.
The -th name is always your friend's name, since today is their name day and therefore it is also exactly one year from now. The names in the input consist only of the characters a-z, contain no spaces, and are at most 10 characters long. All names for the coming year are different.
Output
Output a single integer on one line: the minimum number of days until the next name day, if you help your friend cheat with their name.
Hint
In the first sample, your friend's name is anna. She can make the name anja by replacing the second n in her name with a j, giving her a name day two days earlier than expected. The answer is therefore 3. In the second sample, it is impossible to create another name with the allowed operations above, so jan will have to wait a whole year, 3 days.