Given a string str the task is to expand the string as follows:
If the string is abcd, the resultant string will be d, cd, bcd and abcd in the concatenated form i.e. dcdbcdabcd. We basically need to concatenate all suffixes.
Input: str = “geeks”
Input str = “water”
- Iterate through the string in reverse order i.e. from the last index to the first.
- Print the sub-strings from the current index position till the end of the string.
Below is the implementation of the above approach:
Maintain a suffix string (which is initially empty). Keep traversing from end and keep appending current character.
// C# Implementation of the above approach
// Function to print
// the expansion of the string
static void printExpansion(String str)
String suff = “”;
for (int i = str.Length – 1;
i >= 0; i–)
// Take sub-string from i to n-1
suff = suff + str[i];
// Print the sub-string
// Driver code
public static void Main(String args)
String str = “geeks”;
// This code is contributed by PrinciRaj1992
- Sum of similarities of string with all of its suffixes
- Methods to concatenate string in C/C++ with Examples
- C++ program to concatenate a string given number of times
- TCS Coding Practice Question | Concatenate 2 Strings
- C++ Program to concatenate two strings using Operator Overloading
- Concatenate strings in any order to get Maximum Number of "AB"
- Find the longest sub-string which is prefix, suffix and also present inside the string
- Create a new string by alternately combining the characters of two halves of the string in reverse
- Minimal moves to form a string by adding characters or appending string itself
- String slicing in Python to check if a string can become empty by recursive deletion
- Lexicographically smallest string formed by appending a character from the first K characters of a given string
- Find length of longest subsequence of one string which is substring of another string
- Minimum deletions from string to reduce it to string with at most 2 unique characters
- Find the character in first string that is present at minimum index in second string
- Given a string and an integer k, find the kth sub-string when all the sub-strings are sorted according to the given condition
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.