Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: improve longest_palindromic_subsequence.cpp #2467

Merged
merged 1 commit into from
May 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions dynamic_programming/longest_palindromic_subsequence.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @file
* @brief Program to find the Longest Palindormic
* Subsequence of a string
* @brief Program to find the [Longest Palindormic
* Subsequence](https://www.geeksforgeeks.org/longest-palindromic-subsequence-dp-12/) of a string
*
* @details
* [Palindrome](https://en.wikipedia.org/wiki/Palindrome) string sequence of
Expand All @@ -18,8 +18,15 @@
#include <vector> /// for std::vector

/**
* Function that returns the longest palindromic
* @namespace
* @brief Dynamic Programming algorithms
*/
namespace dynamic_programming {
/**
* @brief Function that returns the longest palindromic
* subsequence of a string
* @param a string whose longest palindromic subsequence is to be found
* @returns longest palindromic subsequence of the string
*/
std::string lps(const std::string& a) {
const auto b = std::string(a.rbegin(), a.rend());
Expand Down Expand Up @@ -70,17 +77,22 @@ std::string lps(const std::string& a) {

return ans;
}
} // namespace dynamic_programming

/** Test function */
void test() {
assert(lps("radar") == "radar");
assert(lps("abbcbaa") == "abcba");
assert(lps("bbbab") == "bbbb");
assert(lps("") == "");
/**
* @brief Self-test implementations
* @returns void
*/
static void test() {
assert(dynamic_programming::lps("radar") == "radar");
assert(dynamic_programming::lps("abbcbaa") == "abcba");
assert(dynamic_programming::lps("bbbab") == "bbbb");
assert(dynamic_programming::lps("") == "");
}

/**
* Main Function
* @brief Main Function
* @returns 0 on exit
*/
int main() {
test(); // execute the tests
Expand Down