![]() |
TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
|
Implementation of Abbrievation More...
#include <cassert>
#include <cstdint>
#include <iostream>
#include <string>
#include <vector>
Go to the source code of this file.
Namespaces | |
namespace | dynamic_programming |
Dynamic Programming algorithms. | |
namespace | abbreviation |
Functions for Abbreviation implementation. |
Functions | |
bool | dynamic_programming::abbreviation::abbreviation_recursion (std::vector< std::vector< bool > > *memo, std::vector< std::vector< bool > > *visited, const std::string &str, const std::string &result, uint32_t str_idx=0, uint32_t result_idx=0) |
Recursive Dynamic Programming function. | |
bool | dynamic_programming::abbreviation::abbreviation (const std::string &str, const std::string &result) |
Iterative Dynamic Programming function. | |
static void | test () |
Self test-implementations. | |
int | main () |
Main function. |
Implementation of Abbrievation
Given two strings, a and b, determine if it's possible to make a equal to b You can perform the following operations on the string a:
The idea is in the problem statement itself: iterate through characters of string a and b (for character indexes i and j respectively):
Time Complexity: (O(|a|*|b|)) where |a| => length of string a
Definition in file abbreviation.cpp.
bool dynamic_programming::abbreviation::abbreviation | ( | const std::string & | str, |
const std::string & | result ) |
Iterative Dynamic Programming function.
Returns whether s can be converted to t with following rules: a. Capitalize zero or more of s's lowercase letters from string s b. remove all other lowercase letters from string s Note: The transition states for iterative is similar to recursive as well
str | given string, which might not be abbreivated |
result | resultant abbreivated string |
Definition at line 119 of file abbreviation.cpp.
bool dynamic_programming::abbreviation::abbreviation_recursion | ( | std::vector< std::vector< bool > > * | memo, |
std::vector< std::vector< bool > > * | visited, | ||
const std::string & | str, | ||
const std::string & | result, | ||
uint32_t | str_idx = 0, | ||
uint32_t | result_idx = 0 ) |
Recursive Dynamic Programming function.
Returns whether s can be converted to t with following rules: a. Capitalize zero or more of a's lowercase letters from string s b. remove all other lowercase letters from string s
memo | To store the result |
visited | boolean to check if the result is already computed |
str | given string, which might not be abbreivated |
result | resultant abbreivated string |
str_idx | index for string str, helpful for transitions |
result_idx | index for string result, helpful for transitions |
(str[i] == result[j]): if str char at position i is equal to result char at position j, then s character is a capitalized one, move on to next character str[i] - 32 == result[j]: if str[i] character is lowercase of result[j] then explore two possibilites:
Definition at line 59 of file abbreviation.cpp.
int main | ( | void | ) |
Main function.
Definition at line 192 of file abbreviation.cpp.
|
static |
Self test-implementations.
Definition at line 153 of file abbreviation.cpp.