Ad
  1. Add some negative/edge test cases.
  2. Convert to recursive solution.
  • I find multiple return statements tricky to follow.
  • This problem lends itself nicely to recursion since we can know the call stack size ahead of time. It is also tail recursive, if C# supports that?
Code
Diff
  • using System;
    
    class Palindrome
    {
      public static bool Check(string word){
        word = word.ToUpper();
        return Check(word, 0);
      }
      
      private static bool Check(string word, int index) {
        var reverseIndex = word.Length - 1 - index;
        return reverseIndex <= index
          ? true
          : word[index] == word[reverseIndex]
            ? Check(word, index + 1)
            : false;
      }
    }
    • using System;
    • class Palindrome
    • {
    • public static bool Check(string word){
    • word = word.ToUpper();
    • for(int i = 0; i < word.Length / 2; i++)
    • {
    • if(word[i] != word[word.Length - 1 - i])
    • {
    • return false;
    • }
    • }
    • return true;
    • return Check(word, 0);
    • }
    • private static bool Check(string word, int index) {
    • var reverseIndex = word.Length - 1 - index;
    • return reverseIndex <= index
    • ? true
    • : word[index] == word[reverseIndex]
    • ? Check(word, index + 1)
    • : false;
    • }
    • }