Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created March 31, 2016 06:14
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/83d3d1a71ff80d6b209f0d6f9aaa435a to your computer and use it in GitHub Desktop.
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
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