Concatenate suffixes of a String

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.

Examples:

Input: str = “geeks”
Output: sksekseeksgeeks



Input str = “water”
Output rerteraterwater

Simple Approach:

  • 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:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to print the expansion of the string
void printExpansion(string str)
{
    int size = 0;
    for (int i = str.length() - 1; i >= 0; i--) {
  
        // Take sub-string from i to n-1
        string subStr = str.substr(i, ++size);
  
        // Print the sub-string
        cout << subStr;
    }
}
  
// Driver code
int main()
{
    string str = "geeks";
    printExpansion(str);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach 
  
public class  GFG {
  
    // Function to print the expansion of the string 
    static void printExpansion(String str) 
    
        for (int i = str.length() - 1; i >= 0; i--) { 
      
            // Take sub-string from i to n-1 
            String subStr = str.substring(i); 
      
            // Print the sub-string 
            System.out.print(subStr); 
  
        
    
  
    // Driver code 
    public static void main(String args[])  
    
        String str = "geeks"
        printExpansion(str); 
      
    
    // This code is contributed by Ryuga
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach
  
# Function to print the expansion of the string
def printExpansion(str):
    for i in range(len(str)-1, -1, -1):
  
        # Take sub-string from i to n-1
        for j in range(i, len(str)):
            print(str[j], end ="")
  
# Driver code
str = "geeks"
printExpansion(str)

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach 
 using System;
class  GFG {
   
    // Function to print the expansion of the string 
    static void printExpansion(String str) 
    
        for (int i = (int)str.Length - 1; i >= 0; i--) { 
       
            // Take sub-string from i to n-1 
            String subStr = str.Substring(i); 
       
            // Print the sub-string 
            Console.Write(subStr); 
   
        
    
   
    // Driver code 
    static public void Main(String []args)  
    
        String str = "geeks"
        printExpansion(str); 
       
    
}
    // This code is contributed by Arnab Kundu

chevron_right


Output:

sksekseeksgeeks

Efficient Approach:
Maintain a suffix string (which is initially empty). Keep traversing from end and keep appending current character.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to print the expansion of the string
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
        cout << suff;
    }
}
  
// Driver code
int main()
{
    string str = "geeks";
    printExpansion(str);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
import java.util.*;
  
class solution
{
  
// 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.charAt(i);
  
        // Print the sub-string
        System.out.print(suff);
    }
}
  
// Driver code
public static void main(String args[])
{
    String str = "geeks";
    printExpansion(str);
  
}
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach
  
# Function to print the expansion 
# of the string
def printExpansion( str):
  
    suff = ""
    for i in range (len (str) - 1, -1, -1) :
  
        # Take sub-string from i to n-1
        suff = suff + str[i]
  
        # Print the sub-string
        print (suff, end = "")
  
# Driver code
if __name__ == "__main__":
  
    str = "geeks"
    printExpansion(str)
  
# This code is contributed by ita_c

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# Implementation of the above approach
using System;
      
class GFG
{
  
// 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
        Console.Write(suff);
    }
}
  
// Driver code
public static void Main(String []args)
{
    String str = "geeks";
    printExpansion(str);
}
}
  
// This code is contributed by PrinciRaj1992 

chevron_right


Output:

sskskeskeeskeeg


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.





Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.