-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path125.IsPalindrome.cs
34 lines (32 loc) · 1.14 KB
/
125.IsPalindrome.cs
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
// 125. 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.
//Approch 1
public class Solution
{
public bool IsPalindrome(string s)
{
Regex rgx = new Regex("[^a-zA-Z]");
s = rgx.Replace(s, "").ToLower();
char[] charArray = s.ToCharArray();
Array.Reverse(charArray);
return s.Equals(new string(charArray));
}
}
//Approch 2
public class Solution
{
public bool IsPalindrome(string s)
{
List<char> strArr = new List<char>();
for (int i = 0; i < s.Length; i++)
{
if ((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z') ||
(s[i] >= '0' && s[i] <= '9'))
strArr.Add(Char.ToLower(s[i]));
}
List<char> revStrArr = Enumerable.Reverse(strArr).ToList();
return (new string(strArr.ToArray())).Equals(new string(revStrArr.ToArray()));
}
}