diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-28 13:42:54 +0100 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-28 13:42:54 +0100 |
commit | 67ba846fd11c2e30cbc315c1e1b048b354cdafc1 (patch) | |
tree | b9b68d906a246c3dd7529dfe7ba2782078453df1 | |
parent | 68ce3daa9e8a48e597a55ca65d274e79f19c0267 (diff) |
-rw-r--r-- | week1/StringReverse.cs | 8 |
1 files changed, 3 insertions, 5 deletions
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)); } } } |