I like to read this solution as a small machine: keep the useful information, throw away the noise. For 680. Valid Palindrome II, the solution in this repository is mainly a greedy solution.
Guide
What?
The first job is to translate the English prompt into state, transition, and stopping conditions. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: greedy.
The notes already sitting in the source point us in the right direction:
- TLE
Guide
When?
This is the kind of solution you want when the problem has structure hiding inside a messy-looking input. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are rValidPalindrome, validPalindrome, isPalindromeRange.
Guide
Why?
The code is doing bookkeeping so your brain does not have to keep the entire search space open at once.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Initialize the memory or helper structure.
- Process candidates in the order the invariant expects.
- Update the answer only when the current state is valid.
- Return the value that represents the fully processed input.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(N) where N is the length of the string.
- Space: O(1) additional complexity. Only pointers were stored in memory.
Guide
C++ Solution
Your submission
The accepted solution
01//TLE
02
03class Solution {
04public:
05 bool rValidPalindrome(string s, int i, int j, int deleted){
06 if(i >= j) return true;
07 if(s[i] == s[j]){
08 return rValidPalindrome(s, i+1, j-1, deleted);
09 }else if(s[i+1] != s[j] && s[i] != s[j-1]){
10 //we cannot make it valid by deleting a char
11 return false;
12 }else if(deleted == 1){
13 //we must delete one more char now, and it exceeds the limit
14 return false;
15 }
16 //delete one char from former part or latter part
17 return rValidPalindrome(s, i+1, j, deleted+1) || rValidPalindrome(s, i, j-1, deleted+1);
18 }
19 bool validPalindrome(string s) {
20 return rValidPalindrome(s, 0, s.size()-1, 0);
21 }
22};
23
24//Greedy
25//Time Complexity: O(N) where N is the length of the string.
26// Each of two checks of whether some substring is a palindrome is O(N).
27//Space Complexity: O(1) additional complexity. Only pointers were stored in memory.
28//Runtime: 112 ms, faster than 71.55% of C++ online submissions for Valid Palindrome II.
29//Memory Usage: 26.1 MB, less than 56.80% of C++ online submissions for Valid Palindrome II.
30
31class Solution {
32public:
33 bool isPalindromeRange(string s, int i, int j){
34 while(i < j){
35 if(s[i++] != s[j--]) return false;
36 }
37 return true;
38 }
39 bool validPalindrome(string s) {
40 for(int i = 0; i < s.size()/2; i++){
41 int j = s.size()-1-i;
42 if(s[i] != s[j]){
43 return isPalindromeRange(s, i+1, j) ||
44 isPalindromeRange(s, i, j-1);
45 }
46 }
47 return true;
48 }
49};
Cost