
- 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
Last Substring in Lexicographical Order in C++
Suppose we have s as string, we have to find the last substring of s in lexicographic order.
So, if the input is like "abbbcabbc", then the output will be "cabbc"
To solve this, we will follow these steps −
i := 0,j := 1,k := 0
while j + k < size of s, do &minsu;
if s[i + k] is same as s[j + k], then −
(increase k by 1)
Ignore following part, skip to the next iteration
if s[i + k] < s[j + k], then −
i := j
(increase j by 1)
Otherwise
j := j + k + 1
k := 0
return substring of s from index i to end
Let us see the following implementation to get better understanding −
Example
#include <bits/stdc++.h> using namespace std; class Solution { public: string lastSubstring(string s) { int i = 0; int j = 1; int k = 0; while(j + k < s.size()){ if(s[i + k] == s[j + k]) { k++; continue; } if(s[i + k] < s[j + k]){ i = j; j++; }else{ j = j + k + 1; } k = 0; } return s.substr(i, s.size() - i); } }; main(){ Solution ob; cout << (ob.lastSubstring("abbbcabbc")); }
Input
"abbbcabbc"
Output
cabbc
- Related Articles
- Sort the words in lexicographical order in C#
- K-th Smallest in Lexicographical Order in C++
- C++ Program to Sort Elements in Lexicographical Order (Dictionary Order)
- Print all longest common sub-sequences in lexicographical order in C++
- How to Sort Elements in Lexicographical Order (Dictionary Order) in Golang?
- Sort the words in lexicographical order in Java
- Sort the words in lexicographical order in Python
- Java Program to Sort Elements in Lexicographical Order (Dictionary Order)
- Swift Program to Sort Elements in Lexicographical Order (Dictionary Order)
- Kotlin Program to Sort Elements in Lexicographical Order (Dictionary Order)
- Haskell Program to Sort Elements in Lexicographical Order (Dictionary Order)
- Print all the combinations of a string in lexicographical order in C++
- Return a sorted array in lexicographical order in JavaScript
- Print all distinct circular strings of length M in lexicographical order in C++
- Lexicographical Numbers in C++

Advertisements