Canna

Tuesday, 25 September 2018

Making Palindrome

  • Problem Description

    Chef likes strings a lot but he likes palindromic strings even more. Today he found an old string s in his garage. The string is so old that some of its characters have faded and are unidentifiable now. Faded characters in the string are represented by '.' whereas other characters are lower case Latin alphabets i.e ['a'-'z'].
    Chef being the palindrome lover decided to construct the lexicographically smallest palindrome by filling each of the faded character ('.') with a lower case Latin alphabet. Can you please help him completing the task?
    Input
    First and the only line of each case contains string s denoting the old string that chef has found in his garage.
    Output
    For each test case, print lexicographically smallest palindrome after filling each faded character - if it possible to construct one. Print -1 otherwise.
  • Test Case 1

    Input (stdin)
    a.ba
    
    
    Expected Output
    abba
  • Test Case 2

    Input (stdin)
    cb.bc
    
    
    Expected Output
    cbabc
  • Program
  • #include<stdio.h>
    #include <string.h>
    
    int main()
    {
     int j,length,flag;
     
     char str[100001];
     scanf("%s",str);
     length=strlen(str);
     
     flag=0;
      for(j=0;j<length;j++)
            {
       
       if(str[j]=='.' && str[length-j-1]=='.')
                {
        str[j]=str[length-j-1]='a';
       }
       else if(str[j]=='.' || str[length-j-1]=='.'){
        
        if(str[j]=='.')
                    {
         str[j]=str[length-j-1]; 
        }
                  else
                    {
         str[length-j-1]=str[j];
        }
       }
              else if(str[j] != str[length-j-1])
              {
                   flag = 1;
                   break;
       }
      }
     if(flag)
        {
      printf("-1\n");
     }
      else
        {
      printf("%s\n",str);
     }
     
     return 0;
    }

No comments:

Post a Comment

Note: only a member of this blog may post a comment.