## makemytrip Interview Question

Java Developers**Country:**India

**Interview Type:**Written Test

I ignored the part where there are T inputs of K. This part depends on the format of the input (text file, variadic function, array, etc.) and should be discussed with the interviewer.

The general process is as follows:

1. Find all substrings of length K in S.

2. For each substring, count the characters. If only one character count is odd, a palindrome can be formed. Return K.

3. If |S| == K, return -1 (no palindrome of length K or greater can be found)

4. Repeat the process for S and K+1.

```
def candy_pal(str,k):
substrings = []
for i in range(len(str) - k + 1):
substrings.append(str[i:i+k])
for substring in substrings:
if has_pal(substring):
return k
if len(str) == k:
return -1
return candy_pal(str,k+1)
def has_pal(str):
char_count = [0] * 26
for char in str:
char_ord = ord(char) - ord('a')
char_count[char_ord] = char_count[char_ord] + 1
has_odd = False
for i in range(len(char_count)):
if char_count[i] % 2 == 1:
if has_odd:
return False
else:
has_odd = True
return True
```

I ignored the part with T inputs, and just considered the case where we have S and K.

The general process is like this:

1. Find all subsets of length K in S.

2. For each subset, count all characters. If only one char count in the substring is odd, a palindrome can be formed. Return K.

3. If |S| == K, return -1

4. Repeat the process for S and K+1.

Solution in Python:

- Anonymous May 06, 2018