Skip to content

Commit

Permalink
Create valid-palindrome-ii.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
kamyu104 authored Sep 17, 2017
1 parent 6831c21 commit 4730949
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions C++/valid-palindrome-ii.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Time: O(n)
// Space: O(1)

class Solution {
public:
bool validPalindrome(string s) {
int left = 0, right = s.length() - 1;
while (left < right) {
if (s[left] != s[right]) {
return validPalindrome(s, left, right - 1) || validPalindrome(s, left + 1, right);
}
++left, --right;
}
return true;
}

private:
bool validPalindrome(const string& s, int left, int right) {
while (left < right) {
if (s[left] != s[right]) {
return false;
}
++left, --right;
}
return true;
}
};

0 comments on commit 4730949

Please sign in to comment.