Shift String
Practice
3 (2 votes)
Breadth First search
Algorithms
Dynamic programming and bit masking
Brute Force search
String
Dynamic programming
Bitmask
Problem
66% Success 528 Attempts 30 Points 2s Time Limit 256MB Memory 1024 KB Max Code

Given a string, \(s\), of length, \(n\), and a list of \(m\) strings, \(x_{1}, x_{2}, ..., x_{m}\).  All \(m\) strings in the list are distinct and have length \(n\) i.e \(\forall i \hspace{0.1cm} |x_{i}| = n\), and none of the given strings is equal to \(s\).

You can perform any number of operations \(s\).

In an operation you are allowed to choose one position in the string, \(s\), and change the character at the position to the next or previous character in the English alphabet cyclically ('a' -> 'b', 'b' -> 'c', 'c' -> 'd', ..., 'z' -> 'a', 'b' -> 'a', ...).

What is the minimum number of operations that you have to perform on, \(s\), so for all the given \(m\) strings, \(s\) becomes \(x_{i}\) at least once after one of the performed operations. 

 

INPUT FORMAT 

The first line of the input contains an integer, \(n\) \((1 <= n <= 5)\)   - denoting the length of the string \(s\)

The second line of the input contains a string, \(s\) - denoting the string which you can perform operations on. Each character in \(s\) is a lowercase English alphabet ('a' - 'z'). 

The next line of the input contains an integer, \(m\) \((1 <= m <= 15)\) - denoting the number of strings that will be in the list.

The next \(m\) lines distinct strings, \(x_{i}\) (\(|x_{i}| = n\), \(x_{i} \neq s\))   - denoting a string in the list. Each character in \(x_{i}\) is a lowercase english alphabet ('a' - 'z')

 

OUTPUT FORMAT 

The output should contain one line denoting the minimum amount of operations that need to be performed on \(s\), to reach each of the \(m\) strings at least once. 

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
14 votes
Tags:
AlgorithmsApprovedBit ManipulationDynamic ProgrammingMedium
Points:30
3 votes
Tags:
AlgorithmsDynamic ProgrammingMedium
Points:30
27 votes
Tags:
AlgorithmsDynamic ProgrammingMedium