Palindrome Index


Problem Statement :


Given a string of lowercase letters in the range ascii[a-z], determine the index of a character that can be removed to make the string a palindrome. There may be more than one solution, but any will do. If the word is already a palindrome or there is no solution, return -1. Otherwise, return the index of a character to remove.

Example

s = "bcbc"

Either remove 'b' at index 0 or 'c' at index 3.


Function Description

Complete the palindromeIndex function in the editor below.

palindromeIndex has the following parameter(s):

string s: a string to analyze

Returns

int: the index of the character to remove or -1.

Input Format

The first line contains an integer q, the number of queries.
Each of the next q lines contains a query string s.



Constraints

1  <=   q  <= 20
1  <=   length of s  <=  10^5 + 5 


All characters are in the range ascii[a-z].


Solution :



title-img


                            Solution in C :

In  C++  :





#include<stdio.h>
#include<string.h>
char a[1000005];
char b[1000005];
int is(char *a, int n){
	int i;
	for(i=0;i<n/2;i++)if(a[i]!=a[n-1-i])return i;
	return -1;
}
int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		
		scanf("%s",&a);
		int l = strlen(a);
		int x = is(a,l);
		if(x==-1){
			printf("-1\n");
		}
		else {
			int i;
			int j=0;
			for(i=0;a[i];i++){
				if(i!=x)b[j++] = a[i];
				
			
			}
				if(is(b,l-1)==-1)printf("%d\n",x);
				else printf("%d\n",l-1-x);
		}
	}
}









In  Java  :






import java.util.*;

public class Solution {
    
    static boolean isPalidrom(String s, int i, int j) {
        while (i < j) {
            if (s.charAt(i++) !=  s.charAt(j--)) {
                return false;
            }
        }
        return true;
    }
    
    
    
    public static void main(String[] args) {
        
        Scanner in = new Scanner(System.in);
        
        int T = in.nextInt();
        
        for (int t = 0; t < T; t++) {
            String s = in.next();
            
            int i = 0;
            int j = s.length() - 1;
            while (i < j) {
                if (s.charAt(i) != s.charAt(j)) {
                    if (isPalidrom(s,i,j-1)) {
                        System.out.println(j);
                    } else {
                        System.out.println(i);
                    }
                    break;
                } 
                i ++;
                j --;
            }
            
            if (i >= j) {
                System.out.println(-1);
            }
        }
        
    }
    
    
    
}








In   C :






#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
    int t,i,flag,len,ans,j,stop;
    char *str;
    str = (char *) malloc(sizeof(char)*100005);
    scanf("%d",&t);
    while(t>0)
    {
        scanf("%s",str);
        len=strlen(str);
        ans=-1;
        stop=0;
        flag=1;
        while(stop==0)
        {
            i=0;
            while(i<len/2 && ans==-1)
            {
                if(str[i]!=str[len-i-1])
                {
                    ans=i;
                    j=i+1;
                    while(j<=len/2 && flag==1)
                    {
                        if(str[j]!=str[len-j])
                        {
                            flag=0;
                        }
                        j++;
                    }
                    if(flag==1)
                    {
                        stop=1;
                    }
                    else
                        flag=1;
                    if(stop==0)
                    {
                        ans=len-i-1;
                        j=i;
                        while(j<=len/2 && flag==1)
                        {
                            if(str[j]!=str[len-j-2])
                            {
                                flag=0;
                            }
                            j++;
                        }
                        if(flag==1)
                        {
                            stop=1;
                        }
                        else
                            flag=1;
                    }
                }
                i++;
            }
            if(ans==-1)
            {
                stop=1;
            }
        }
        printf("%d\n",ans);
        t--;
    }
    return 0;
}








In   Python3  :








t = int(input())

def is_palindrome(s):
	return s == s[::-1]

def solve(s):
	i, j = 0, len(s) - 1
	while (i < j) and (s[i] == s[j]):
		i += 1
		j -= 1
	if i == j:
		return 0
	if is_palindrome(s[i + 1 : j + 1]):
		return i
	if is_palindrome(s[i:j]):
		return j
	raise AssertionError

for _ in range(t):
	print(solve(input()))
                        




View More Similar Problems

Print the Elements of a Linked List

This is an to practice traversing a linked list. Given a pointer to the head node of a linked list, print each node's data element, one per line. If the head pointer is null (indicating the list is empty), there is nothing to print. Function Description: Complete the printLinkedList function in the editor below. printLinkedList has the following parameter(s): 1.SinglyLinkedListNode

View Solution →

Insert a Node at the Tail of a Linked List

You are given the pointer to the head node of a linked list and an integer to add to the list. Create a new node with the given integer. Insert this node at the tail of the linked list and return the head node of the linked list formed after inserting this new node. The given head pointer may be null, meaning that the initial list is empty. Input Format: You have to complete the SinglyLink

View Solution →

Insert a Node at the head of a Linked List

Given a pointer to the head of a linked list, insert a new node before the head. The next value in the new node should point to head and the data value should be replaced with a given value. Return a reference to the new head of the list. The head pointer given may be null meaning that the initial list is empty. Function Description: Complete the function insertNodeAtHead in the editor below

View Solution →

Insert a node at a specific position in a linked list

Given the pointer to the head node of a linked list and an integer to insert at a certain position, create a new node with the given integer as its data attribute, insert this node at the desired position and return the head node. A position of 0 indicates head, a position of 1 indicates one node away from the head and so on. The head pointer given may be null meaning that the initial list is e

View Solution →

Delete a Node

Delete the node at a given position in a linked list and return a reference to the head node. The head is at position 0. The list may be empty after you delete the node. In that case, return a null value. Example: list=0->1->2->3 position=2 After removing the node at position 2, list'= 0->1->-3. Function Description: Complete the deleteNode function in the editor below. deleteNo

View Solution →

Print in Reverse

Given a pointer to the head of a singly-linked list, print each data value from the reversed list. If the given list is empty, do not print anything. Example head* refers to the linked list with data values 1->2->3->Null Print the following: 3 2 1 Function Description: Complete the reversePrint function in the editor below. reversePrint has the following parameters: Sing

View Solution →