Created
March 31, 2016 06:14
-
-
Save jianminchen/83d3d1a71ff80d6b209f0d6f9aaa435a to your computer and use it in GitHub Desktop.
Palindrome index - using iterative solution, not recursive; stop early once found one possible index
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; | |
class Solution { | |
static void Main(string[] args) | |
{ | |
int t = int.Parse(Console.ReadLine()); | |
for (int i = 0; i < t; i++) | |
{ | |
Console.WriteLine(FindCharacterToRemove(Console.ReadLine())); | |
} | |
} | |
static int FindCharacterToRemove(string s) | |
{ | |
var high = s.Length; | |
for (var low = 0; low < high--; low++) | |
{ | |
if (s[low] != s[high]) | |
{ | |
return s[low + 1] == s[high] && (s.Length < 3 || s[low + 2] == s[high - 1]) ? low : high; | |
} | |
} | |
return -1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment