3 Palindromes | Code Vita 2020 Question and Solution | Code Vita 9

 Problem Description

In this 3 Palindrome, Given an input string word, split the string into exactly 3 palindromic substrings. Working from left to right, choose the smallest split for the first substring that still allows the remaining word to be split into 2 palindromes. Similarly, choose the smallest second palindromic substring that leaves a third palindromic substring.

If there is no way to split the word into exactly three palindromic substrings, print “Impossible” (without quotes). Every character of the string needs to be consumed.

Cases not allowed –

After finding 3 palindromes using above instructions, if any character of the original string remains unconsumed.

No character may be shared in forming 3 palindromes.

Constraints

1 <= the length of input sting <= 1000

Input

First line contains the input string consisting of characters between [a-z].

Output

Print 3 substrings one on each line.

Time Limit

1

Example 1

Input

nayannamantenet

Output

nayan

naman

tenet

Explanation

The original string can be split into 3 palindromes as mentioned in the output.

However, if the input was nayanamantenet, then the answer would be “Impossible”.


Example 2

Input

aaaaa

Output

a

a

aaa

Explanation

The other ways to split the given string into 3 palindromes are as follows –

[a, aaa, a], [aaa, a, a], [aa, aa, a], etc.

Since we want to minimize the length of the first palindromic substring using left to right processing, the correct way to split is [a, a, aaa].


Example 3

Input

aaaabaaaa

Output

a

aaabaaa

a

Explanation

The other ways to split the given string into 3 palindromes are as follows –

[aaaa, b, aaaa], [aa, aabaa, aa], etc.

Since we want to minimize the length of the first palindromic substring using left to right processing, the correct way to split is [a, aaabaaa, a].


Solution in  C:

#include <stdio.h>

#define mod 1000000007
#define MAX_LENGTH 1000

typedef long long int lld;

int isPalindrome(char str[], int start, int end) {
while (start < end) {
if (str[start] != str[end]) {
return 0; // Not a palindrome
}
start++;
end--;
}
return 1; // Palindrome
}

int main() {
char s[MAX_LENGTH], s1[MAX_LENGTH], s2[MAX_LENGTH], s3[MAX_LENGTH];
scanf("%s", s);

int l = 0;
while (s[l] != '\0') {
l++;
}

for (int i = 1; i < l - 1; i++) {
for (int k = 0; k < i; k++) {
s1[k] = s[k];
}
s1[i] = '\0';

if (isPalindrome(s, 0, i - 1)) {
for (int j = 1; j < l - i; j++) {
for (int k = 0; k < j; k++) {
s2[k] = s[i + k];
}
s2[j] = '\0';

for (int k = 0; k < l - i - j; k++) {
s3[k] = s[i + j + k];
}
s3[l - i - j] = '\0';

if (isPalindrome(s2, 0, j - 1) && isPalindrome(s3, 0, l - i - j - 1)) {
printf("%s\n%s\n%s\n", s1, s2, s3);
return 0;
}
}
}
}

printf("Impossible\n");
return 0;
}



You can also run it on an online IDE: 

https://ide.geeksforgeeks.org/online-python3-compiler/294545fa-8a51-46da-ab50-8fb862187677

Your feedback are always welcome! If you have any doubt you can contact me or leave a comment!  Happy Coding !! Cheers!!!

Related Links:



No comments:

Post a Comment

Super Market Problem | TCS Code Vita 2023 - Zone 1 | Super Market TCS Code Vita 2023 Solution | Code Vita 2023 | Code Vita 2023 season 11 solution

 Problem Description: In a Super market we will find many variations of the same product. In the same way we can find many types of rice bag...