- Your solution looks fine;
- Overall, we would want to stay away from regular expressions in solving algorithm problems – especially easy LeetCode questions are to measure yourour implementation skills;
- C# has two simple functions that would do the job of that regular expression(
\W|_
):
- We can just a bit improve the runtime, but not so much, using a more readable expression
(?i)[^a-z0-9]+
:We can just a bit improve the runtime, but not so much, using a more readable expression such as:
(?i)[^a-z0-9]+
;(?i)
matches the remainder of the pattern based on the insensitive flag;
[^A-Za-z0-9]+
;
Demo
- Your solution looks fine;
- Overall, we would want to stay away from regular expressions in solving algorithm problems – especially easy LeetCode questions are to measure your implementation skills;
- C# has two simple functions that would do the job of that regular expression:
- We can just a bit improve the runtime, but not so much, using a more readable expression
(?i)[^a-z0-9]+
:
- Your solution looks fine;
- Overall, we would want to stay away from regular expressions in solving algorithm problems – especially easy LeetCode questions are to measure our implementation skills;
- C# has two simple functions that would do the job of (
\W|_
):
We can just a bit improve the runtime, but not so much, using a more readable expression such as:
(?i)[^a-z0-9]+
;(?i)
matches the remainder of the pattern based on the insensitive flag;
[^A-Za-z0-9]+
;
Demo
Disclaimer: Not a Code ReviewerCode Reviewer
Happy Coding! ( •ˆ_ˆ• )
Disclaimer: Not a Code Reviewer
Disclaimer: Not a Code Reviewer
Happy Coding! ( •ˆ_ˆ• )
- We can just a bit improve the runtime, but not so much, using a more readable expression
(?i)[^a-z0-9]+
:
public class Solution {
public bool IsPalindrome(string s) {
s = System.Text.RegularExpressions.Regex.Replace(s, @"(?i)[^a-z0-9]"9]+", "");
if (s == null || s == "" || s.Length == 1 || s == " ") {
return true;
}
for (int i = 0, j = s.Length - 1; i <= s.Length / 2 && j >= s.Length / 2; i++, j--) {
if (Char.ToLower(s[i]) == Char.ToLower(s[j])) {
continue;
}
return false;
}
return true;
}
}```
RegEx Circuit
jex.im visualizes regular expressions:
If you wish to simplify/update/explore the expression, it's been explained on the top right panel of regex101.com . You can watch the matching steps or modify them in this debugger link , if you'd be interested. The debugger demonstrates that how a RegEx engine might step by step consume some sample input strings and would perform the matching process.
- We can just a bit improve the runtime, but not so much:
public class Solution {
public bool IsPalindrome(string s) {
s = System.Text.RegularExpressions.Regex.Replace(s, @"(?i)[^a-z0-9]", "");
if (s == null || s == "" || s.Length == 1 || s == " ") {
return true;
}
for (int i = 0, j = s.Length - 1; i <= s.Length / 2 && j >= s.Length / 2; i++, j--) {
if (Char.ToLower(s[i]) == Char.ToLower(s[j])) {
continue;
}
return false;
}
return true;
}
}```
- We can just a bit improve the runtime, but not so much, using a more readable expression
(?i)[^a-z0-9]+
:
public class Solution {
public bool IsPalindrome(string s) {
s = System.Text.RegularExpressions.Regex.Replace(s, @"(?i)[^a-z0-9]+", "");
if (s == null || s == "" || s.Length == 1 || s == " ") {
return true;
}
for (int i = 0, j = s.Length - 1; i <= s.Length / 2 && j >= s.Length / 2; i++, j--) {
if (Char.ToLower(s[i]) == Char.ToLower(s[j])) {
continue;
}
return false;
}
return true;
}
}
RegEx Circuit
jex.im visualizes regular expressions:
If you wish to simplify/update/explore the expression, it's been explained on the top right panel of regex101.com . You can watch the matching steps or modify them in this debugger link , if you'd be interested. The debugger demonstrates that how a RegEx engine might step by step consume some sample input strings and would perform the matching process.