Your friend isn't too good with computers so as a practical joke someone scrambled the letters (a-z) on his keyboard. When he sit down and tried to type his name looking at the keyboard he realized that the letters are scrambled and asked your help.
You are smart so you know that if he types his name and then repeatedly retypes what comes up on the screen instead of his name he will succeed inputting in his name eventually. You are also kind and rearrange the keys but want to know how many turns would it take to succeed.
Your task is to write a program or function that given the shuffling of the letters and the friend's name computes the number of turns.
- Two string are given as input in a structure convenient for your language.
- The first string is the list of the new lowercase letters in alphabetical order of the old ones. (The first character is the one that is at the position of
a, last one is at the position of
z.) Some change will always occur in the string.
- The second string is the name. It could contain any printable ascii character but only the upper and lowercase alphabetical characters will be shuffled if any. The name itself might not get shuffled at al.
- Output is a single integer the number of turns minimally required. Newline is optional.
'abcfdeghijklmnopqrstuvwxyz' 'Mr. John Doe' (d, e, f positions changed)
3 (The shown names are:
Mr. John Fod =>
Mr. John Eof =>
Mr. John Doe )
'nopqrstuvwxyzabcdefghijklm' 'Mr. John Doe' (the ROT13 cipher)
2 (Any input name that contains letters will take
2 rounds to produce the original name.)
'aebcdjfghiqklmnopzrstuvwxy' 'John Doe'
This is code-golf so the shortest entry wins.