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
- Concatenate strings in any order to get Maximum Number of "AB"
- TCS Coding Practice Question | Concatenate 2 Strings
- C++ Program to concatenate two strings using Operator Overloading
- Lexicographically smallest string formed by appending a character from the first K characters of a given string
- Lexicographically smallest string formed by appending a character from first K characters of a string | Set 2
- Create a new string by alternately combining the characters of two halves of the string in reverse
- Find the longest sub-string which is prefix, suffix and also present inside the string
- String Range Queries to find the number of subsets equal to a given String
- String slicing in Python to check if a string can become empty by recursive deletion
- Minimal moves to form a string by adding characters or appending string itself
- Find length of longest subsequence of one string which is substring of another string
- Find the count of palindromic sub-string of a string in its sorted form
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.