Valid Palindrome Error
Feb 12, 2020 at 11:53pm UTC
Given a non-empty string s, you may delete at most one character. Judge whether you can make it a palindrome.
Example 1:
Input: "aba"
Output: True
Example 2:
Input: "abca"
Output: True
Explanation: You could delete the character 'c'.
Note:
The string will only contain lowercase characters a-z. The maximum length of the string is 50000
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
class Solution {
public :
bool validPalindrome(string s)
{
int left = 0;
int right = s.length()-1;
while (left<=right)
{
if (s[left]==s[right])
{
left++;
right--;
}
else
{
return (isPalindrome(left+1,right,s)||isPalindrome(left,right-1,s));
}
}
return true ;
}
bool isPalindrome(int a, int b, string str)
{
while (a<=b)
{
if (str[a]!=str[b])
{
return false ;
}
a++;
b--;
}
return true ;
}
};
Error: Line 506: Char 9: runtime error: pointer index expression with base 0x000000000000 overflowed to 0xffffffffffffffff (basic_string.h)
What am I missing here?
Feb 13, 2020 at 12:54am UTC
You're missing a complete program that we can run with the push of a button.
And you're missing the input that caused the problem.
BTW, C++ is not Java!
Don't stick your functions in a class for no reason.
If you want a namespace, use a namespace.
Feb 13, 2020 at 1:03am UTC
This seems to be equivalent to what you wrote, and I don't get any errors.
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
#include <iostream>
#include <string>
using namespace std;
bool isPalindrome(string s, int left, int right)
{
while (left < right)
if (s[left++] != s[right--])
return false ;
return true ;
}
bool nearPalindrome(string s)
{
int left = 0, right = s.length() - 1;
while (left < right)
if (s[left++] != s[right--])
return isPalindrome(s, left, right+1)
|| isPalindrome(s, left-1, right );
return true ;
}
int main()
{
cout << nearPalindrome("" ) << '\n' ;
cout << nearPalindrome("aba" ) << '\n' ;
cout << nearPalindrome("abca" ) << '\n' ;
cout << nearPalindrome("abcdedcxba" ) << '\n' ;
cout << nearPalindrome("abcdyedcxba" ) << '\n' ;
}
Here's your original code (with the spacing fixed):
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 44 45 46 47 48 49 50 51
#include <iostream>
#include <string>
using namespace std;
class Solution {
public :
bool validPalindrome(string s)
{
int left = 0;
int right = s.length()-1;
while (left <= right)
{
if (s[left] == s[right])
{
left++;
right--;
}
else
{
return isPalindrome(left+1, right, s)
|| isPalindrome(left, right-1, s);
}
}
return true ;
}
bool isPalindrome(int a, int b, string str)
{
while (a <= b)
{
if (str[a] != str[b])
{
return false ;
}
a++;
b--;
}
return true ;
}
};
int main()
{
Solution s;
cout << s.validPalindrome("" ) << '\n' ;
cout << s.validPalindrome("aba" ) << '\n' ;
cout << s.validPalindrome("abca" ) << '\n' ;
cout << s.validPalindrome("abcdedcxba" ) << '\n' ;
cout << s.validPalindrome("abcdyedcxba" ) << '\n' ;
}
Last edited on Feb 13, 2020 at 1:05am UTC
Topic archived. No new replies allowed.