Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created March 31, 2016 06:06
Show Gist options
  • Save jianminchen/db16371dda11652a7dfee6d577f2221f to your computer and use it in GitHub Desktop.
Save jianminchen/db16371dda11652a7dfee6d577f2221f to your computer and use it in GitHub Desktop.
PalindromeIndex -
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace theLoveLetterMstery
{
class Program
{
static void Main(string[] args)
{
int n = Convert.ToInt16(Console.ReadLine());
for(int i=0;i<n;i++)
{
Console.WriteLine(indexPalindrome(Console.ReadLine()));
}
}
// 1 < len < 100005
public static int indexPalindrome(string s)
{
int len = s.Length;
if (isPalindrome(s, -1))
return -1;
for(int i = 0;i<len;i++)
{
if (isPalindrome(s, i))
return i;
}
return -1;
}
private static bool isPalindrome(string s, int index)
{
int len = s.Length;
int start = 0;
int end = len - 1;
while (start <= end)
{
if (start == index)
start++;
else if (end == index)
end--;
else if (s[start] == s[end])
{
start++;
end--;
}
else
return false;
}
return true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment