Created
March 31, 2016 06:17
-
-
Save jianminchen/44ee14c6a45f25f94e11e892baad857c to your computer and use it in GitHub Desktop.
Palindrome Index - iterative solution
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) { | |
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