Advertisement
YavorJS

Palindrome_Index

Aug 9th, 2016
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace _9.Palindrome_Index
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. List<char> str = Console.ReadLine().ToList();
  14. bool palindromeOrNot = isItAPalindrome(str);
  15.  
  16. if (palindromeOrNot)
  17. {
  18. Console.WriteLine("-1");
  19. }
  20. else
  21. {
  22. for (int ch = 0; ch < str.Count; ch++)
  23. {
  24. char oldStr = str[ch];
  25. str.RemoveAt(ch);
  26. palindromeOrNot = isItAPalindrome(str);
  27. if (palindromeOrNot)
  28. {
  29. Console.WriteLine(ch);
  30. break;
  31. }
  32. str.Insert(ch, oldStr);
  33. }
  34. }
  35. }
  36.  
  37. private static bool isItAPalindrome(List<char> str)
  38. {
  39. int rightIndex = str.Count - 1;
  40. int leftIndex = 0;
  41. while (rightIndex >= leftIndex)
  42. {
  43. if (str[rightIndex] != str[leftIndex])
  44. {
  45. return false;
  46. }
  47. rightIndex--;
  48. leftIndex++;
  49. }
  50. return true;
  51. }// end of isItAPalindrome
  52. }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement