📜  连接字符串的后缀

📅  最后修改于: 2021-05-31 23:47:51             🧑  作者: Mango

给定字符串str ,任务是按如下所示扩展字符串:
如果字符串是abcd ,则结果字符串将是dcdbcdabcd ,并为连接形式,即dcdbcdabcd 。我们基本上需要连接所有后缀。

例子:

简单方法:

  • 以相反的顺序遍历字符串,即从最后一个索引到第一个索引。
  • 从当前索引位置开始打印子字符串,直到字符串。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
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;
}


Java
// 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
}


Python3
# 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)


C#
// 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


C++
// C++ implementation of the approach
#include 
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;
}


Java
// 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);
  
}
}


Python3
# 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


C#
// 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


输出:
sksekseeksgeeks

高效方法:
保留后缀字符串(最初为空)。保持从头开始遍历,并附加当前字符。

C++

// C++ implementation of the approach
#include 
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;
}

Java

// 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);
  
}
}

Python3

# 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

C#

// 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 
输出:
sskskeskeeskeeg
想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”