Check if "madam" is a palindrome.
Ready — edit the code above and click Run.
using System;
class Program
{
static void Main()
{
string s = "madam";
int l = 0, r = s.Length - 1;
bool ok = true;
while (l < r)
{
if (s[l++] != s[r--]) { ok = false; break; }
}
Console.WriteLine(ok ? "Palindrome" : "Not Palindrome");
}
}
Try solving on your own first, then reveal the official answer.
Two-pointer compare from both ends—O(n) time, O(1) extra space.