-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
29 lines (24 loc) · 900 Bytes
/
Solution.cs
File metadata and controls
29 lines (24 loc) · 900 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
namespace LeetCode.Problem125{
//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) {
Queue<char> forward = new Queue<char>();
Stack<char> backward = new Stack<char>();
for (int i = 0; i < s.Length; i++)
{
if (char.IsLetterOrDigit(s[i]))
{
forward.Enqueue(char.ToLower(s[i]));
backward.Push(char.ToLower(s[i]));
}
}
return forward.SequenceEqual(backward);
}
}
}