-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathValidPalindrome.cs
55 lines (50 loc) · 1.8 KB
/
ValidPalindrome.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using System.Text;
using System.Text.RegularExpressions;
namespace LeetCodeSolutionsLib
{
/// <summary>
/// 125. Valid Palindrome
/// Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
/// IE) Input: "A man, a plan, a canal: Panama"
/// Output: true
/// </summary>
public class ValidPalindrome : Solution
{
private string _inputString;
public ValidPalindrome(string inputString)
{
this._inputString = inputString;
}
private bool isPalindrome(string inputString)
{
// Regex to only consider alphanumeric chars
Regex rgx = new Regex("[^a-zA-Z0-9]");
inputString = rgx.Replace(inputString.ToLower(), "");
// Compare each chars from the start and end
// If its a palindrome then the characters should be the same.
int counter = inputString.Length - 1;
for (int i = 0; i <= inputString.Length - 1; i++)
{
if (inputString[i] != inputString[counter--])
{
return false;
}
}
return true;
}
public override void PrintExample()
{
var watch = System.Diagnostics.Stopwatch.StartNew();
var results = isPalindrome(this._inputString);
watch.Stop();
Console.WriteLine($"125. Valid Palindrome\n" +
$"Input String = {this._inputString} \n" +
$"Result: [{results}] \n" +
$"Execution Speed: {watch.ElapsedMilliseconds}ms \n");
}
}
}