### PROBLEM LINK:

[Practice][1]

[Contest][2]

**Author:** [Praveen Dhinwa][3]

**Tester:** [Sergey Kulik][4]

**Editorialist:** [Mugurel Ionut Andreica][5]

### DIFFICULTY:

EASY-MEDIUM

### PREREQUISITES:

[Arithmetic progression][8], [Hamming distance][9]

### PROBLEM:

Given a string **s**, the alphabet size **A** and a number **K**, find out how many *good* strings **w** of the same length as **s** exist, such that the Hamming distance between **s** and **w** is at most **K**. A string **w** is *good* if for every **3** positions **i<j<k** such that **j-i=k-j** the condition **w(i)=w(j)=w(k)** is **false**.

### EXPLANATION:

**A** is at most 3 and the maximum length of **s** is 50. If you try to generate all the *good* strings for every alphabet size up to 3 and every length up to 50 you will notice that there aren’t too many of them.

Thus, you can generate all these strings in the beginning. Then, for every test case, just iterate through all the good strings of the same length as **s** and for the given alphabet size. For each good string **w** you just need to compute its Hamming distance to **s**. If this distance is at most equal to **K**, then you increment a counter. The answer for each test case is the value of the counter.

### AUTHOR’S AND TESTER’S SOLUTIONS:

Author’s solution can be found here.

Tester’s solution can be found here.

[1]: http://www.codechef.com/problems/DEVGOSTR

[2]: http://www.codechef.com/APRIL16/problems/DEVGOSTR

[3]: http://www.codechef.com/users/admin2

[4]: http://www.codechef.com/users/xcwgf666

[5]: http://www.codechef.com/users/mugurelionut

[6]: X

[7]: Y

[8]: https://en.wikipedia.org/wiki/Arithmetic_progression

[9]: https://en.wikipedia.org/wiki/Hamming_distance