
- 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
C++ code to find two substrings with one minimal substring
Suppose we have a lowercase string S with n characters. We have to find two non-empty substrings P and Q, such that −
Both P and Q are subsequences of S
For each index i, S[i] belong to exactly one of P and Q.
P is lexicographically minimum as possible.
So, if the input is like S = "thelightsaber", then the output will be 10, because we need 2 red notebooks, 3 green notebooks, and 5 blue notebooks.
Steps
To solve this, we will follow these steps −
c := S sort the array c a := position of (c[0]) in S delete c from S print c[0] and S
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; void solve(string S){ string c = S; sort(c.begin(), c.end()); int a = S.find(c[0]); S.erase(S.begin() + a); cout << c[0] << ", " << S << endl; } int main(){ string S = "thelightsaber"; solve(S); }
Input
"thelightsaber"
Output
a, thelightsber
- Related Articles
- C++ code to find minimal tiredness after meeting
- C++ Program to find minimal sum of all MEX of substrings
- C++ code to find palindrome string whose substring is S
- C++ code to find string where trygub is not a substring
- C++ code to count number of even substrings of numeric string
- Replace substring with another substring C++
- C++ code to find screen size with n pixels
- Longest Substring with At Most Two Distinct Characters in C++
- Find the Number of Substrings of One String Present in Other using C++
- C++ code to find sorted array with non-divisibility conditions
- C++ code to find array from given array with conditions
- C++ code to find minimum moves with weapons to kill enemy
- C++ code to get two numbers in range x with given rules
- C++ code to find pair of numbers where one is multiple of other
- C# Program to find all substrings in a string

Advertisements