
g0601_0700.s0647_palindromic_substrings.Solution.cpp Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-all Show documentation
Show all versions of leetcode-in-all Show documentation
104 LeetCode algorithm problem solutions
The newest version!
// #Medium #String #Dynamic_Programming #Big_O_Time_O(n^2)_Space_O(n)
// #2024_05_21_Time_3_ms_(94.97%)_Space_7.5_MB_(85.16%)
#include
#include
class Solution {
private:
void expand(const std::vector& a, int l, int r, int& res) {
while (l >= 0 && r < a.size()) {
if (a[l] != a[r]) {
return;
} else {
res++;
l--;
r++;
}
}
}
public:
int countSubstrings(const std::string& s) {
std::vector a(s.begin(), s.end());
int res = 0;
for (int i = 0; i < a.size(); i++) {
expand(a, i, i, res);
expand(a, i, i + 1, res);
}
return res;
}
};
© 2015 - 2025 Weber Informatics LLC | Privacy Policy