
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Generalized Abbreviation in C++
Suppose there is a word. We have to define a function that can generate the generalized abbreviations of a word.
So, if the input is like "word", then the output will be ["word", "1ord", "w1rd", "wo1d", "wor1", "2rd", "w2d", "wo2", "1o1d", "1or1", "w1r1", "1o2", "2r1", "3d", "w3", "4"]
To solve this, we will follow these steps −
Define an array ret
Define a function solve(), this will take s, idx,
if idx >= size of s, then −
insert s at the end of ret
return
y := substring of s from index 0 to idx - 1
i := size of y
num := blank string
while (i >= 0 and y[i] <= ASCII of '9' and y[i] >= ASCII of '0'), do −
num := y[i] + num
(decrease i by 1)
if i is not equal to size of y, then −
ret := substring of s from index (0 to idx - (size of y - 1 - i) - 1) concatenate (number + 1)as string concatenate substring of s from index (0 to idx)
s1 := num + 1 as string
s2 := num as string
if size of s1 is same as size of s2, then −
Otherwise
solve(ret, idx + 1)
Otherwise
prev := s[idx]
s[idx] := '1'
solve(s, idx + 1)
s[idx] := prev
solve(s, idx + 1)
From the main method do the following −
solve(word, 0)
return ret
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; void print_vector(vector<auto< v){ cout << "["; for(int i = 0; i<v.size(); i++){ cout << v[i] << ", "; } cout << "]"<<endl; } class Solution { public: vector<string> ret; void solve(string s, int idx){ if (idx >= s.size()) { ret.push_back(s); return; } string y = s.substr(0, idx); int i = y.size() - 1; string num = ""; while (i >= 0 && y[i] <= '9' && y[i] >= '0') { num = y[i] + num; i--; } if (i != y.size() - 1) { string ret = s.substr(0, idx - (y.size() - 1 - i)) + to_string(stoi(num) + 1) + s.substr(idx + 1); string s1 = to_string(stoi(num) + 1); string s2 = to_string(stoi(num)); if (s1.size() == s2.size()) solve(ret, idx); else solve(ret, idx + 1); } else { char prev = s[idx]; s[idx] = '1'; solve(s, idx + 1); s[idx] = prev; } solve(s, idx + 1); } vector<string< generateAbbreviations(string word){ solve(word, 0); return ret; } }; main(){ Solution ob; print_vector(ob.generateAbbreviations("hello")); }
Input
hello
Output
[5, 4o, 3l1, 3lo, 2l2, 2l1o, 2ll1, 2llo, 1e3, 1e2o, 1e1l1, 1e1lo, 1el2, 1el1o, 1ell1, 1ello, h4, h3o, h2l1, h2lo, h1l2, h1l1o, h1ll1, h1llo, he3, he2o, he1l1, he1lo, hel2, hel1o, hell1, hello, ]
- Related Articles
- Generalized Anxiety Disorder
- Generalized Lists in Data Structure
- Word Abbreviation in C++
- Generalized Lambda Expressions in C++14
- What are Generalized Linear Models?
- HTML DOM Abbreviation Object
- Minimum Unique Word Abbreviation in C++
- How to include an abbreviation in HTML?
- Can inherited properties of objects be generalized?
- How to mark abbreviation or acronyms in HTML?
- How to get the US states name abbreviation in R?
- How do I get a human-readable file size in bytes abbreviation using C#?
- State whether the following are true or false. Justify your answer.( cos A ) is the abbreviation used for the cosecant of angle ( A . )
- State whether the following statements are true or false. Justify your answer.(i) The value of $tan A$ is always less than 1.(ii) $sec A = frac{12}{5}$ for some value of angle A.(iii) $cos A$ is the abbreviation used for the cosecant of angle A.(iv) $cot A$ is the product of cot and A.(v) $sin θ = frac{4}{3}$ for some angle.
- Make strings in array become keys in object in a new array in JavaScript?
