some more neetcode

This commit is contained in:
2023-10-20 20:58:44 -07:00
parent 42c44952c5
commit df57a62275
8 changed files with 97 additions and 2 deletions

View File

@@ -0,0 +1,10 @@
#include "soln.hpp"
int main()
{
string s = "A man, a plan, a canal: Panama";
bool answer = true;
Solution soln;
cout << "Checked palindrome correctly? " << soln.test(s, answer) << endl;
return 0;
}

View File

@@ -0,0 +1,32 @@
#include "soln.hpp"
bool Solution::isPalindrome(string s)
{
int n = s.length();
if (n < 2)
return true;
for (int i = 0; i < n; i++)
{
if (s[i] >= 'A' && s[i] <= 'Z')
s[i] += 32;
if ((s[i] < 'a' || s[i] > 'z') &&
(s[i] < '0' || s[i] > '9'))
{
s.erase(s.begin() + i);
i--;
n--;
}
}
n = s.length();
for (int i = 0, j = n - 1; i < n / 2; i++, j--)
{
if (s[i] != s[n - i - 1])
return false;
}
return true;
}
bool Solution::test(string s, bool answer)
{
return isPalindrome(s) == answer;
}

View File

@@ -0,0 +1,8 @@
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
bool isPalindrome(string s);
bool test(string s, bool answer);
};