Quick actions

cmd+k|ctrl+k

Navigation

Languages

Test palindrome

Snippet info

Language

Csharp

Visibility

public

Author

patrykstachowiak

Created

2025-03-11T10:48:24.556264Z

Updated

2025-03-11T10:48:24.556264Z

// Entry point name must be "Solution"
using System;

public static class Solution
{
    private static void Main()
    {
        Console.WriteLine("Hello, world!");
        
        if (IsPalindrome("kayak"))
        {
            Console.WriteLine("Is palindrome");
        }
    }
    
    private static bool IsPalindrome(string str)
    {
        if (str.Length == 0) return true;
        
        int leftIndex = 0;
        int rightIndex = str.Length - 1;
        
        while (leftIndex < rightIndex)
        {
            if (!Char.IsLetterOrDigit(str[leftIndex]))
            {
                leftIndex++;
                continue;
            }
            
            if (!Char.IsLetterOrDigit(str[rightIndex]))
            {
                rightIndex--;
                continue;
            }
            
            if (Char.IsLetterOrDigit(str[leftIndex]) != Char.IsLetterOrDigit(str[rightIndex]))
            {
                return false;
            }
            
            leftIndex++;
            rightIndex--;
        }
        
        return true;
    }
}
INFO