summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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));
}
}
}