Cocubes Coding Question – 8

You’re given a function,

char * ConvertToPalindrome(char* str)

The function accepts a string str, implement the function to find and return the minimum characters required to append at the end of str to make it a palindrome

Assumptions – 

The string will only contain lowercase English Alphabets

Note – 

  • If string is already a palindrome then return NULL
  • You have to find the minimum characters required to append at the end of the string to make it a palindrome

Example –

Input – 

abcdc

Output –

ba

 

Please write the program in the comments it will be added here later once the solution is provided in comments in various languages.

9 comments on “Cocubes Coding Question – 8”


  • Prachi

    #If string is already a palindrome then return NULL
    #You have to find the minimum characters required to append at the end of the string to make it a palindrome
    n=input()
    l=list(n)
    print(l)

    if n[:]==n[::-1]:
    print(“NULL”)
    else:
    le=len(l)
    for i in range(le):
    le=len(l)
    if l[i]!=l[le-i-1]:
    l.insert(le-i,n[i])
    print(l)
    s=””.join(l)
    print(“”.join(l))
    if s==s[::-1]:
    s=s.replace(n,””)
    print(s)


  • Shruti

    s=input()
    l=len(s)
    for i in range(l):
    s1=s+s[i::-1]
    if s1[:]==s1[::-1]:
    print(s[i::-1])
    break
    s1=s


  • Vignesh

    import java.util.*;
    public class pallindromappend{
    static boolean pallindrom(String str){
    boolean pal=false;
    StringBuilder sb=new StringBuilder(str);
    sb.reverse();
    String str1=String.valueOf(sb);
    if(str.equals(str1))
    pal=true;
    return pal;
    }
    public static void main(String args[]){
    Scanner s=new Scanner(System.in);
    String st=s.next();
    int n=st.length();
    String ans=””;
    int j=n-1;
    for(int i=0;i<n-1;i++){
    String str=st.substring(i);
    if(pallindrom(str)){
    j=i;
    break;
    }
    }
    if(j!=0){
    ans+=st.substring(0,j);
    StringBuilder ans1=new StringBuilder(ans);
    ans1.reverse();
    System.out.println(ans1);
    }
    else
    System.out.println("NULL");
    }
    }


  • Priyal

    //simple cpp soln using stack
    #include
    using namespace std;
    void solve(){
    int n,m;
    string s,ans,s1;
    stackc;
    cin>>s1;
    n=s1.length();
    s=s1;

    for(int i=0;i<n;i++){
    c.push(s[i]);
    }
    for(int i=0;i<n;i++){
    if(s[i]!=c.top()){
    continue;
    }
    else{
    m=i;
    goto here;
    }
    }
    if(m==0){
    cout<=0;i–){
    ans+=s[i];
    }
    cout<<ans<<endl;
    }

    int main()
    {

    solve();

    return 0;
    }


  • Mohd Wael

    public class Main {

    public static void main(String[] args) {
    try(Scanner sc = new Scanner(System.in);){
    StringBuilder sb = new StringBuilder(sc.nextLine());
    StringBuilder s = new StringBuilder();
    Boolean finish=true;
    while(finish) {
    if(isPalindrome(sb))
    finish=false;
    else {
    s.append(sb.charAt(0));
    sb=new StringBuilder(sb.substring(1));
    }
    }
    System.out.println(s.reverse());
    }
    }
    public static Boolean isPalindrome(StringBuilder sb) {
    int l = sb.length()/2;
    int m = sb.length() – 1;
    for(int i = 0; i<=l; i++) {
    if(!(sb.charAt(i)==sb.charAt(m-i)))
    return false;
    }
    return true;
    }

    }


  • shivam mishra

    #pyhton program to convert palondrome after adding minimum character in string
    s1=input().strip()
    l=list(s1)
    le=len(l)
    for i in range(le):
    le=len(l)
    if l[i]!=l[le-i-1]:
    l.insert(le-i,l[i])
    else:
    break
    s=””.join(l)
    if s==s[::-1]:
    s=s.replace(s1,””)
    print(s)


  • Subha

    import java.util.*;
    public class Main
    {
    static int append(String s){
    int l =s.length();
    int j=l-1,count=0;
    for(int i =0;i<l;i++)
    {
    while(s.charAt(i)!=s.charAt(j)){
    i++;
    count++;
    }
    j–;
    }
    return count;
    }
    public static void main(String[] args) {
    // TODO code application logic here
    Scanner sc =new Scanner(System.in);
    String s=sc.nextLine();
    int n=append(s);
    StringBuffer str=new StringBuffer(s.substring(0,n));
    System.out.println(str.reverse());
    }


  • AYUSH KUMAR

    import java.util.*;
    /**
    *
    * @author AYUSH
    */
    public class Appends {

    static boolean pal(char[] c){
    int n=c.length;
    char ptr1=c[0];
    char ptr2=c[n-1];
    while(ptr2>ptr1){
    if(ptr1!=ptr2)
    return false;
    ptr1++;
    ptr2++;
    }
    return true;

    }
    static int append(String s){
    if(pal(s.toCharArray()))
    return 0;
    s=s.substring(1);
    return 1+append(s);
    }
    public static void main(String[] args) {
    // TODO code application logic here
    Scanner sc =new Scanner(System.in);
    String s=sc.nextLine();
    int n=append(s);
    StringBuffer str=new StringBuffer(s.substring(0,n));
    System.out.println(str.reverse());
    }

    }