

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Program to check string can be reduced to 2022 or not
<p>Suppose we have a numeric string S with n digits. We perform the following operation with the string S, no more than once. We select two numbers i and j (1 ≤ i ≤ j ≤ n) and removes characters from S string from positions i to j. We have to check whether the string S can be reduced to 2022 in no more than one operations or not.</p><h2>Problem Category</h2><p>To solve this problem, we need to manipulate strings. Strings in a programming language are a stream of characters that are stored in a particular array-like data type. Several languages specify strings as a specific data type (eg. Java, C++, Python); and several other languages specify strings as a character array (eg. C). Strings are instrumental in programming as they often are the preferred data type in various applications and are used as the datatype for input and output. There are various string operations, such as string searching, substring generation, string stripping operations, string translation operations, string replacement operations, string reverse operations, and much more. Check out the links below to understand how strings can be used in C/C++.</p><p><a href="https://www.tutorialspoint.com/cplusplus/cpp_strings.htm" rel="nofollow" target="_blank">https://www.tutorialspoint.com/cplusplus/cpp_strings.htm</a></p><p><a href="https://www.tutorialspoint.com/cprogramming/c_strings.htm" rel="nofollow" target="_blank">https://www.tutorialspoint.com/cprogramming/c_strings.htm</a></p><p>So, if the input of our problem is like S = "2548022", then the output will be True, because we can remove from index 1 to 3.</p><h2>Steps</h2><p>To solve this, we will follow these steps −</p><pre class="just-code notranslate language-cpp" data-lang="cpp">n := size of S for initialize i := 0, when i <= 4, update (increase i by 1), do: temp := (substring of S from 0 to ith character) concatenate (substring of S from index up to [n - 4 + i] if temp is same as "2022", then: return true return false</pre><h2>Example</h2><p>Let us see the following implementation to get better understanding −</p><pre class="demo-code notranslate language-cpp" data-lang="cpp">#include <bits/stdc++.h> using namespace std; bool solve(string S){ int n = S.size(); for (int i = 0; i <= 4; i++){ string temp = S.substr(0, i) + S.substr(n - 4 + i); if (temp == "2022") return true; } return false; } int main(){ string S = "2548022"; cout << solve(S) << endl; }</pre><h2>Input</h2><pre class="result notranslate">2548022</pre><h2>Output</h2><pre class="result notranslate">1</pre>
- Related Questions & Answers
- Program to check a string can be split into three palindromes or not in Python
- Program to check a string can be broken into given list of words or not in python
- Program to check whether final string can be formed using other two strings or not in Python
- Program to check whether one point can be converted to another or not in Python
- Program to check subarrays can be rearranged from arithmetic sequence or not in Python
- C++ Program to check if two stacks of letters can be emptied or not
- C++ Program to check given candies can be split with equal weights or not
- Program to check two strings can be equal by swapping characters or not in Python
- Program to check words can be found in matrix character board or not in Python
- Program to check n can be shown as sum of k or not in Python
- C++ code to check all bulbs can be turned on or not
- C# program to check if string is panagram or not
- C++ Program to check string is strictly alphabetical or not
- Program to check we can replace characters to make a string to another string or not in C++
- Program to check string contains consecutively descending string or not in Python
Advertisements