-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution680.cs
43 lines (37 loc) · 929 Bytes
/
Solution680.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
using LeetCode.DataStructures;
namespace LeetCode.Solutions;
public class Solution680
{
/// <summary>
/// 680. Valid Palindrome II - Easy
/// <a href="https://leetcode.com/problems/valid-palindrome-ii">See the problem</a>
/// </summary>
public bool ValidPalindrome(string s)
{
var left = 0;
var right = s.Length - 1;
while (left < right)
{
if (s[left] != s[right])
{
return IsPalindrome(s, left + 1, right) || IsPalindrome(s, left, right - 1);
}
left++;
right--;
}
return true;
}
private static bool IsPalindrome(string s, int left, int right)
{
while (left < right)
{
if (s[left] != s[right])
{
return false;
}
left++;
right--;
}
return true;
}
}