Created
March 19, 2019 11:54
-
-
Save arun-cm/d46451f3a7760d9efa751e32d95d1bc4 to your computer and use it in GitHub Desktop.
Hacker Rank Palindrome Index Problem Solution C#
This file contains hidden or 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.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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment