Check if a string is a palindrome

后端 未结 30 1561
星月不相逢
星月不相逢 2020-11-28 08:59

I have a string as input and have to break the string in two substrings. If the left substring equals the right substring then do some logic.

How can I do this?

30条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-28 09:40

    This C# method will check for even and odd length palindrome string (Recursive Approach):

    public static bool IsPalindromeResursive(int rightIndex, int leftIndex, char[] inputString)
    {
        if (rightIndex == leftIndex || rightIndex < leftIndex)
            return true;
        if (inputString[rightIndex] == inputString[leftIndex])
            return IsPalindromeResursive(--rightIndex, ++leftIndex, inputString);
        else
            return false;            
    }
    

提交回复
热议问题