Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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