summaryrefslogtreecommitdiff
path: root/week1
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-10-28 13:42:54 +0100
committerLoek Le Blansch <loek@pipeframe.xyz>2024-10-28 13:42:54 +0100
commit67ba846fd11c2e30cbc315c1e1b048b354cdafc1 (patch)
treeb9b68d906a246c3dd7529dfe7ba2782078453df1 /week1
parent68ce3daa9e8a48e597a55ca65d274e79f19c0267 (diff)
fix non-recursive is_palindrome implementationHEADmaster
Diffstat (limited to 'week1')
-rw-r--r--week1/StringReverse.cs8
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));
}
}
}