Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
abbreviation.cpp File Reference

Implementation of Abbrievation More...

#include <cassert>
#include <cstdint>
#include <iostream>
#include <string>
#include <vector>
Include dependency graph for abbreviation.cpp:

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.
 

Detailed Description

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:

  1. Capitalize zero or more of a's lowercase letters.
  2. Delete all of the remaining lowercase letters in a.

Algorithm

The idea is in the problem statement itself: iterate through characters of string a and b (for character indexes i and j respectively):

  1. If a[i] and b[j] are equal, then move to next position
  2. If a[i] is lowercase of b[j], then explore two possibilities: a. Capitalize a[i] or b. Skip a[i]
  3. If the a[i] is not uppercase, just discard that character, else return false

Time Complexity: (O(|a|*|b|)) where |a| => length of string a

Author
Ashish Daulatabad

Function Documentation

◆ abbreviation()

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

Parameters
strgiven string, which might not be abbreivated
resultresultant abbreivated string
Returns
false if string str cannot be converted to result
true if string str can be converted to result
119 {
121 str.size() + 1, std::vector<bool>(result.size() + 1, false));
122
123 for (uint32_t i = 0; i <= str.size(); ++i) {
124 memo[i][0] = true;
125 }
126 for (uint32_t i = 1; i <= result.size(); ++i) {
127 memo[0][i] = false;
128 }
129 for (uint32_t i = 1; i <= str.size(); ++i) {
130 for (uint32_t j = 1; j <= result.size(); ++j) {
131 if (str[i - 1] == result[j - 1]) {
132 memo[i][j] = memo[i - 1][j - 1];
133 } else if (str[i - 1] - 32 == result[j - 1]) {
134 memo[i][j] = (memo[i - 1][j - 1] || memo[i - 1][j]);
135 } else {
136 if (str[i - 1] >= 'A' && str[i - 1] <= 'Z') {
137 memo[i][j] = false;
138 } else {
139 memo[i][j] = memo[i - 1][j];
140 }
141 }
142 }
143 }
144 return memo.back().back();
145}
uint64_t result(uint64_t n)
Definition fibonacci_sum.cpp:77
T size(T... args)
Here is the call graph for this function:

◆ abbreviation_recursion()

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

Parameters
memoTo store the result
visitedboolean to check if the result is already computed
strgiven string, which might not be abbreivated
resultresultant abbreivated string
str_idxindex for string str, helpful for transitions
result_idxindex for string result, helpful for transitions
Returns
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:

  1. convert it to capitalized letter and move both to next pointer (i + 1, j + 1)
  2. Discard the character (str[i]) and move to next char (i + 1, j)
62 {
63 bool ans = memo->at(str_idx).at(result_idx);
64 if (str_idx == str.size() && result_idx == result.size()) {
65 return true;
66 } else if (str_idx == str.size() && result_idx != result.size()) {
67 // result `t` is not converted, return false
68 return false;
69 } else if (!visited->at(str_idx).at(result_idx)) {
70 /**
71 * `(str[i] == result[j])`: if str char at position i is equal to
72 * `result` char at position j, then s character is a capitalized one,
73 * move on to next character `str[i] - 32 == result[j]`:
74 * if `str[i]` character is lowercase of `result[j]` then explore two
75 * possibilites:
76 * 1. convert it to capitalized letter and move both to next pointer
77 * `(i + 1, j + 1)`
78 * 2. Discard the character `(str[i])` and move to next char `(i + 1,
79 * j)`
80 */
81 if (str[str_idx] == result[result_idx]) {
82 ans = abbreviation_recursion(memo, visited, str, result,
83 str_idx + 1, result_idx + 1);
84 } else if (str[str_idx] - 32 == result[result_idx]) {
85 ans = abbreviation_recursion(memo, visited, str, result,
86 str_idx + 1, result_idx + 1) ||
87 abbreviation_recursion(memo, visited, str, result,
88 str_idx + 1, result_idx);
89 } else {
90 // if `str[i]` is uppercase, then cannot be converted, return
91 // `false`
92 // else `str[i]` is lowercase, only option is to discard this
93 // character
94 if (str[str_idx] >= 'A' && str[str_idx] <= 'Z') {
95 ans = false;
96 } else {
97 ans = abbreviation_recursion(memo, visited, str, result,
98 str_idx + 1, result_idx);
99 }
100 }
101 }
102 (*memo)[str_idx][result_idx] = ans;
103 (*visited)[str_idx][result_idx] = true;
104 return (*memo)[str_idx][result_idx];
105}
bool 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.
Definition abbreviation.cpp:59
T at(T... args)
Here is the call graph for this function:

◆ main()

int main ( void )

Main function.

Returns
0 on exit
192 {
193 test(); // run self-test implementations
194 return 0;
195}
static void test()
Self test-implementations.
Definition abbreviation.cpp:153
Here is the call graph for this function:

◆ test()

static void test ( )
static

Self test-implementations.

Returns
void
153 {
154 std::string s = "daBcd", t = "ABC";
156 std::vector<bool>(t.size() + 1, false)),
157 visited(s.size() + 1, std::vector<bool>(t.size() + 1, false));
158
159 assert(dynamic_programming::abbreviation::abbreviation_recursion(
160 &memo, &visited, s, t) == true);
161 assert(dynamic_programming::abbreviation::abbreviation(s, t) == true);
162 s = "XXVVnDEFYgYeMXzWINQYHAQKKOZEYgSRCzLZAmUYGUGILjMDET";
163 t = "XXVVDEFYYMXWINQYHAQKKOZEYSRCLZAUYGUGILMDETQVWU";
165 s.size() + 1, std::vector<bool>(t.size() + 1, false));
166
168 s.size() + 1, std::vector<bool>(t.size() + 1, false));
169
170 assert(dynamic_programming::abbreviation::abbreviation_recursion(
171 &memo, &visited, s, t) == false);
172 assert(dynamic_programming::abbreviation::abbreviation(s, t) == false);
173
174 s = "DRFNLZZVHLPZWIupjwdmqafmgkg";
175 t = "DRFNLZZVHLPZWI";
176
178 s.size() + 1, std::vector<bool>(t.size() + 1, false));
179
181 s.size() + 1, std::vector<bool>(t.size() + 1, false));
182
183 assert(dynamic_programming::abbreviation::abbreviation_recursion(
184 &memo, &visited, s, t) == true);
185 assert(dynamic_programming::abbreviation::abbreviation(s, t) == true);
186}
Here is the call graph for this function: