Files
obsidian/АиСД/Задачи/LeetCode/Valid Palindrome II.md
T
roma-dxunvrs 0e6e583e6d
deploy / Pull and Restart (push) Successful in 17s
refactor: wiki links -> md links
2026-09-22 08:52:23 +03:00

35 lines
1.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
Leetcode #680 | Easy | [Два указателя](../../Методы/Два%20указателя.md)
## Идея
Продолжение [Valid Palindrome](Valid%20Palindrome.md), только теперь можно удалить один символ. При первом несовпадении запускаем вспомогательную функцию, которая допроверяет либо с `l+1 до r`, либо с `l до r-1`
## Big-O
- Время ```O(N)```
- Память ```O(1)```
## Код
```Java
class Solution {
public boolean validPalindrome(String s) {
int l = 0;
int r = s.length()-1;
while (l < r) {
if (s.charAt(l) != s.charAt(r)) {
return isPalindrome(s, l+1, r) || isPalindrome(s, l, r-1);
}
l++;
r--;
}
return true;
}
boolean isPalindrome(String s, int l, int r) {
while (l < r) {
if (s.charAt(l) != s.charAt(r)) {
return false;
}
l++;
r--;
}
return true;
}
}
```