-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
39 lines (31 loc) · 943 Bytes
/
Solution.cs
File metadata and controls
39 lines (31 loc) · 943 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
using System.Text;
namespace LeetCode.Problem125.Alternative{
//125. Valid Palindrome
//https://leetcode.com/problems/valid-palindrome/
/*
A phrase is a palindrome if, after converting all uppercase letters into lowercase letters
and removing all non-alphanumeric characters,
it reads the same forward and backward. Alphanumeric characters include letters and numbers.
Given a string s, return true if it is a palindrome, or false otherwise.
*/
public class Solution {
public bool IsPalindrome(string s) {
StringBuilder sb = new();
foreach (var item in s)
{
if (System.Char.IsLetterOrDigit(item))
sb.Append(Char.ToLower(item));
}
int left = 0;
int right = sb.Length - 1;
while (right >= left)
{
if (sb[left] != sb[right])
return false;
left++;
right--;
}
return true;
}
}
}