
- 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
Check if a given string is sum-string in C++
Here we will see how to check whether a string is sum-string or not. A string is said to be sum-string if the rightmost substring can be written as the sum of two substrings before it, and the same is recursively true for substring before it. Suppose a string like 12243660 is a sum string, like 12 + 24 = 36, and the 36 is present after 12 and 24 in the string, again 24 + 36 = 60, this is also present in the string.
A string S can be called sum-string, if it follows this rule −
𝑠𝑢𝑏𝑠𝑡𝑟𝑖𝑛𝑔(𝑖,𝑥)+𝑠𝑢𝑏𝑠𝑡𝑟𝑖𝑛𝑔(𝑥+1,𝑗)= 𝑠𝑢𝑏𝑠𝑡𝑟𝑖𝑛𝑔(𝑗+1,𝑙)
𝑎𝑛𝑑 𝑠𝑢𝑏𝑠𝑡𝑟𝑖𝑛𝑔(𝑥+1,𝑗)+𝑠𝑢𝑏𝑠𝑡𝑟𝑖𝑛𝑔(𝑗+1,𝑙)= 𝑠𝑢𝑏𝑠𝑡𝑟𝑖𝑛𝑔(𝑙+1,𝑚) 𝑎𝑛𝑑 𝑠𝑜 𝑜𝑛
Example
#include <bits/stdc++.h> using namespace std; string get_string_sum(string str1, string str2) { if (str1.size() < str2.size()) swap(str1, str2); int len1 = str1.size(); int len2 = str2.size(); string ans = ""; int carry = 0; for (int i = 0; i < len2; i++) { int ds = ((str1[len1 - 1 - i] - '0') + (str2[len2 - 1 - i] - '0') + carry) % 10; carry = ((str1[len1 - 1 - i] - '0') + (str2[len2 - 1 - i] - '0') + carry) / 10; ans = char(ds + '0') + ans; } for (int i = len2; i < len1; i++) { int ds = (str1[len1 - 1 - i] - '0' + carry) % 10; carry = (str1[len1 - 1 - i] - '0' + carry) / 10; ans = char(ds + '0') + ans; } if (carry) ans = char(carry + '0') + ans; return ans; } bool sumStrCheckHelper(string str, int beg, int len1, int len2) { string sub1 = str.substr(beg, len1); string sub2 = str.substr(beg + len1, len2); string sum = get_string_sum(sub1, sub2); int sum_len = sum.size(); if (sum_len > str.size() - len1 - len2 - beg) return false; if (sum == str.substr(beg + len1 + len2, sum_len)) { if (beg + len1 + len2 + sum_len == str.size()) return true; return sumStrCheckHelper(str, beg + len1, len2, sum_len); } return false; } bool isSumStr(string str) { int n = str.size(); for (int i = 1; i < n; i++) for (int j = 1; i + j < n; j++) if (sumStrCheckHelper(str, 0, i, j)) return true; return false; } int main() { if(isSumStr("1212243660")) cout << "This is sum-string"; else cout << "This is not sum-string"; }
Output
This is sum-string
- Related Articles
- Python - Check if a given string is binary string or not
- Check if a given string is a valid number in Python
- Check if a given string is a valid number in C++
- C Program to Check if a Given String is a Palindrome?
- Check if a given string is a rotation of a palindrome in C++
- Python program to check if a given string is number Palindrome
- Java program to check if a Substring is present in a given String
- Python Program to check if a substring is present in a given string.
- C# program to check if a Substring is present in a Given String
- Check if a string starts with given word in PHP
- Check if a string ends with given word in PHP
- Python program to check if the given string is pangram
- C program to check if a given string is Keyword or not?
- Python program to check if a given string is Keyword or not
- Swift Program to check if a given string is Keyword or not

Advertisements