From 67ba846fd11c2e30cbc315c1e1b048b354cdafc1 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Mon, 28 Oct 2024 13:42:54 +0100 Subject: fix non-recursive is_palindrome implementation --- week1/StringReverse.cs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) (limited to 'week1/StringReverse.cs') diff --git a/week1/StringReverse.cs b/week1/StringReverse.cs index 2d09b0f..3a06186 100644 --- a/week1/StringReverse.cs +++ b/week1/StringReverse.cs @@ -8,11 +8,9 @@ namespace ALGA { } public static bool is_palindrome(String s) { - for (int front = 0; front < s.Length; front++) { - int back = s.Length - 1 - front; - if (s[front] != s[back]) return false; - } - return true; + if (s.Length < 2) return true; + if (s[0] != s[s.Length - 1]) return false; + return is_palindrome(s.Substring(1, s.Length - 2)); } } } -- cgit v1.2.3