Skip to content

Instantly share code, notes, and snippets.

@arun-cm
Created March 19, 2019 11:54
Show Gist options
  • Select an option

  • Save arun-cm/d46451f3a7760d9efa751e32d95d1bc4 to your computer and use it in GitHub Desktop.

Select an option

Save arun-cm/d46451f3a7760d9efa751e32d95d1bc4 to your computer and use it in GitHub Desktop.

Revisions

  1. arun-cm created this gist Mar 19, 2019.
    71 changes: 71 additions & 0 deletions PalindromeIndex.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,71 @@
    using System.CodeDom.Compiler;
    using System.Collections.Generic;
    using System.Collections;
    using System.ComponentModel;
    using System.Diagnostics.CodeAnalysis;
    using System.Globalization;
    using System.IO;
    using System.Linq;
    using System.Reflection;
    using System.Runtime.Serialization;
    using System.Text.RegularExpressions;
    using System.Text;
    using System;

    class Solution {

    // Complete the palindromeIndex function below.
    static int palindromeIndex(string s)
    {
    int maxIndex = s.Length/2;

    for (int i = 0; i < maxIndex; i++)
    {
    if(s[i] != s[s.Length -1 - i])
    {
    if(s.Substring(i + 1, 2) == Reverse(s.Substring((s.Length - 1 - i)-1, 2)) )
    {
    return i;
    }
    else
    {
    return (s.Length - 1 - i);
    }

    return i;
    }
    }

    return -1;
    }

    static string Reverse(string str)
    {
    string reverse = "";
    int Length = 0;
    Length = str.Length - 1;
    while(Length>=0)
    {
    reverse = reverse + str[Length];
    Length--;
    }
    return reverse;
    }

    static void Main(string[] args) {
    TextWriter textWriter = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);

    int q = Convert.ToInt32(Console.ReadLine());

    for (int qItr = 0; qItr < q; qItr++) {
    string s = Console.ReadLine();

    int result = palindromeIndex(s);

    textWriter.WriteLine(result);
    }

    textWriter.Flush();
    textWriter.Close();
    }
    }