Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created March 31, 2016 06:11
Show Gist options
  • Save jianminchen/3817befd0be48a396c1998a776bf76b5 to your computer and use it in GitHub Desktop.
Save jianminchen/3817befd0be48a396c1998a776bf76b5 to your computer and use it in GitHub Desktop.
Palindrome index - 14 lines of code
using System;
using System.Collections.Generic;
using System.IO;
class Solution {
static void Main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */
int T = int.Parse(Console.ReadLine());
for(int t = 0; t < T; t++)
{
string test = Console.ReadLine();
Console.WriteLine(FindIndex(test, 0, test.Length-1));
}
}
static int FindIndex(string test, int s, int e)
{
int i =s, j= e;
while(i < j)
{
if(test[i] != test[j])
{
// which one to remove?
return FindIndex(test, i+1, j) == -1 ? i : j;
}
i++;j--;
}
return -1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment