Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created March 31, 2016 06:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/44ee14c6a45f25f94e11e892baad857c to your computer and use it in GitHub Desktop.
Save jianminchen/44ee14c6a45f25f94e11e892baad857c to your computer and use it in GitHub Desktop.
Palindrome Index - iterative solution
using System;
using System.Collections.Generic;
using System.IO;
class Solution {
static void Main(String[] args) {
int numTests = int.Parse(Console.ReadLine());
for(int t = 0; t < numTests; ++t) {
string s = Console.ReadLine();
int i = 0, j = s.Length - 1;
int remove_pos = -1;
while(i < j) {
if(s[i] != s[j]) {
// detect if we need to remove i or j
remove_pos = s[i+1] == s[j] && s[i+2] == s[j-1] ? i : j;
break;
}
i++;
j--;
}
Console.WriteLine(remove_pos);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment