Created
March 31, 2016 06:11
-
-
Save jianminchen/3817befd0be48a396c1998a776bf76b5 to your computer and use it in GitHub Desktop.
Palindrome index - 14 lines of code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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