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
:
a
's lowercase letters.a
.The idea is in the problem statement itself: iterate through characters of string a
and b
(for character indexes i
and j
respectively):
a[i]
and b[j]
are equal, then move to next positiona[i]
is lowercase of b[j]
, then explore two possibilities: a. Capitalize a[i]
or b. Skip a[i]
a[i]
is not uppercase, just discard that character, else return false
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 |
false
if string str
cannot be converted to result
true
if string str
can be converted to result
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 |
false
if string str
cannot be converted to result
true
if string str
can be converted to result
(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:
(i + 1, j + 1)
(str[i])
and move to next char (i + 1, j)
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.