当前位置:网站首页>Leetcode (680) -- verifying palindrome string II
Leetcode (680) -- verifying palindrome string II
2022-06-29 23:51:00 【SmileGuy17】
Leetcode(680)—— Verify palindrome string Ⅱ
subject
Given a non empty string s, Delete at most one character . Determine whether it can be a palindrome string .
Example 1:
Input : s = “aba”
Output : true
Example 2:
Input : s = “abca”
Output : true
explain : You can delete c character .
Example 3:
Input : s = “abc”
Output : false
Tips :
- 1 1 1 <= s.length <= 1 0 5 10^5 105
- s It's made up of lowercase letters
Answer key
Method 1 : greedy ( Double pointer )
Ideas
Consider the simplest approach : First, judge whether the original string is a palindrome string , If it is , Just go back to true \text{true} true; If not , Then enumerate each location as the deleted location , Then judge whether the remaining string is a palindrome string . The incremental time complexity of this approach is O ( n 2 ) O(n^2) O(n2) Of , Will exceed the time limit .
Let's change our mind . First consider if deleting characters is not allowed , How to determine whether a string is a palindrome string . A common practice is to use double pointers . Define left and right pointers , Initially, it points to the first character and the last character of the string , Judge whether the characters pointed to by the left and right pointers are the same each time , If it's not the same , It's not a palindrome string ; If the same , Move the left and right pointers to the middle by one bit , Until the left and right pointers meet , Then the string is a palindrome string .
When a maximum of one character is allowed to be deleted , You can also use double pointers , Achieve... Through greed . Initialize two pointers low \textit{low} low and high \textit{high} high Points to the first character and the last character of the string, respectively . Judge whether the characters pointed to by the two pointers are the same each time , If the same , Then update the pointer , take low \textit{low} low Add 1 1 1, high \textit{high} high reduce 1 1 1, Then judge whether the substring within the updated pointer range is a palindrome string . If two pointers point to different characters , Then one of the two characters must be deleted , At this point, we are divided into two situations : That is, delete the character corresponding to the left pointer , Leave a substring s [ low + 1 : high ] s[\textit{low} + 1 : \textit{high}] s[low+1:high], Or delete the character corresponding to the right pointer , Leave a substring s [ low : high − 1 ] s[\textit{low} : \textit{high} - 1] s[low:high−1]. When at least one of the two substrings is a palindrome string , It means that the original string will become a palindrome string after deleting a character .
The greedy strategy is : Left and right pointers encounter different characters , Delete one of them , And then continue to traverse .

Code implementation
My own
class Solution {
bool checkValidPalindrome(string& s, int l, int r){
while(l < r){
if(s[l] == s[r]){
l++;
r--;
}else return false;
}
return true;
}
public:
bool validPalindrome(string s) {
// Reverse double pointer
int l = 0, r = s.size()-1;
while(l < r){
if(s[l] == s[r]){
l++;
r--;
}else return checkValidPalindrome(s, l+1, r) || checkValidPalindrome(s, l, r-1);
// Because I'm not sure which is the string that blocks the palindrome string , So they all traverse , The wrong one will exit the function soon , It doesn't take long
}
return true;
}
};
Complexity analysis
Time complexity : O ( n ) O(n) O(n), among nn Is the length of the string . The time complexity of judging whether the whole string is a palindrome string is O ( n ) O(n) O(n), When encountering different characters , The time complexity of judging whether the two substrings are palindrome strings is also O ( n ) O(n) O(n)
Spatial complexity : O ( 1 ) O(1) O(1). You only need to maintain a limited constant space
边栏推荐
- Incluxdb time series database system
- Set up security groups, record domain names, and apply for SSL certificates
- Solr basic operation
- Yunhe enmo, gaiguoqiang, identify it and grasp it before the domestic database boils
- The concept and significance of mean, variance, standard deviation and covariance
- shell-位置参数变量和预定义变量
- C pointer advanced 1-- > character pointer, array pointer, pointer and array parameter transfer, function pointer
- Fund valuation, expenses and accounting
- 25 interview questions about Apache
- 穿越过后,她说多元宇宙真的存在
猜你喜欢

Construction of module 5 of actual combat Battalion

HPE launched ARM CPU general server product
![[Shangshui Shuo series] day 8](/img/66/2aaa82f122612db1775bdd45556d97.png)
[Shangshui Shuo series] day 8

剑指 Offer 13. 机器人的运动范围

小程序插件接入、开发与注意事项

Et la tarte aux framboises 4? Quels sont les jeux possibles?

雲和恩墨蓋國强,識別它、抓住它,在國產數據庫沸騰以前
![[wechat applet] understand the basic composition of the applet project](/img/71/98894fbb9cda4facfd2b83c8ec8f9a.png)
[wechat applet] understand the basic composition of the applet project

为什么 JSX 语法这么香?

Yunhe enmo Guoqiang, identifiez - le, saisissez - le, avant l'ébullition de la base de données nationale
随机推荐
The concept and significance of mean, variance, standard deviation and covariance
Solr basic operation
CE second operation
[LeetCode] 只出现一次的数字【136】
[Shangshui Shuo series] day 8
C pointer advanced 1-- > character pointer, array pointer, pointer and array parameter transfer, function pointer
FPGA开发(1)——串口通信
剑指 Offer 14- II. 剪绳子 II
网上开户选哪个证券公司?还有,在线开户安全么?
matplotlib matplotlib可视化之柱状图plt.bar()
招商证券靠谱吗?开股票账户安全吗?
Et la tarte aux framboises 4? Quels sont les jeux possibles?
Redis client
海外数字身份验证服务商ADVANCE.AI入选EqualOcean《2022品牌出海服务市场研究报告》
How about counting Berry Pie 4? What are the possible ways to play?
Sword finger offer 38 Arrangement of strings
机器学习:VC维的概念和用途
Solution to version conflict of flutter plug-in
请指教同花顺软件究竟是什么?究竟网上开户是否安全么?
Leetcode 1385. Distance value between two arrays