📜  打印“n”行中之字形字符串的串联

📅  最后修改于: 2022-05-13 01:57:07.444000             🧑  作者: Mango

打印“n”行中之字形字符串的串联

给定一个字符串和行数'n'。当输入字符串以逐行之字形方式写入时,打印通过连接 n 行形成的字符串。

例子:

Input: str = "ABCDEFGH"
       n = 2
Output: "ACEGBDFH"
Explanation: Let us write input string in Zig-Zag fashion
             in 2 rows.
A   C   E   G   
  B   D   F   H
Now concatenate the two rows and ignore spaces 
in every row. We get "ACEGBDFH"

Input: str = "GEEKSFORGEEKS"
       n = 3
Output: GSGSEKFREKEOE
Explanation: Let us write input string in Zig-Zag fashion
             in 3 rows.
G       S       G       S
  E   K   F   R   E   K
    E       O       E
Now concatenate the two rows and ignore spaces 
in every row. We get "GSGSEKFREKEOE"

我们强烈建议您单击此处并进行练习,然后再继续使用解决方案。

这个想法是遍历输入字符串。每个字符都必须转到其中一行。将所有字符一一添加到不同的行。下面是算法:

1) Create an array of n strings, arr[n]
2) Initialize direction as "down" and row as 0. The 
   direction indicates whether we need to move up or 
   down in rows. 
3) Traverse the input string, do following for every
   character.
   a) Append current character to string of current row.
   b) If row number is n-1, then change direction to 'up'
   c) If row number is 0, then change direction to 'down'
   d) If direction is 'down', do row++.  Else do row--.
4) One by one print all strings of arr[]. 

下面是上述想法的实现。

C++
// C++ program to print string obtained by concatenation
// of different rows of Zig-Zag fashion
 
#include
using namespace std;
 
// Prints concatenation of all rows of str's Zig-Zag fashion
void printZigZagConcat(string str, int n)
{
    // Corner Case (Only one row)
    if (n == 1)
    {
        cout << str;     
        return;
    }  
 
    // Find length of string
    int len = str.length();
 
    // Create an array of strings for all n rows
    string arr[n];
 
    // Initialize index for array of strings arr[]
    int row = 0;
    bool down; // True if we are moving down in rows,
               // else false
 
    // Traverse through given string
    for (int i = 0; i < len; ++i)
    {
        // append current character to current row
        arr[row].push_back(str[i]);
 
        // If last row is reached, change direction to 'up'
        if (row == n-1)
          down = false;
 
        // If 1st row is reached, change direction to 'down'
        else if (row == 0)
          down = true;
 
        // If direction is down, increment, else decrement
        (down)? (row++): (row--);
    }
 
    // Print concatenation of all rows
    for (int i = 0; i < n; ++i)
        cout << arr[i];
}
 
// Driver program
int main()
{
    string str = "GEEKSFORGEEKS";
    int n = 3;
    printZigZagConcat(str, n);
    return 0;
}


Java
// Java program to print string
// obtained by concatenation
// of different rows of
// Zig-Zag fashion
import java.util.Arrays;
 
class GFG {
 
    // Prints concatenation
    // of all rows of str's
    // Zig-Zag fashion
    static void printZigZagConcat(String str,
            int n)
    {
 
        // Corner Case (Only one row)
        if (n == 1)
        {
            System.out.print(str);
            return;
        }
        char[] str1 = str.toCharArray();
 
        // Find length of string
        int len = str.length();
 
        // Create an array of
        // strings for all n rows
        String[] arr = new String[n];
        Arrays.fill(arr, "");
 
        // Initialize index for
        // array of strings arr[]
        int row = 0;
        boolean down = true; // True if we are moving
        // down in rows, else false
 
        // Traverse through
        // given string
        for (int i = 0; i < len; ++i)
        {
            // append current character
            // to current row
            arr[row] += (str1[i]);
 
            // If last row is reached,
            // change direction to 'up'
            if (row == n - 1)
            {
                down = false;
            }
             
            // If 1st row is reached,
            // change direction to 'down'
            else if (row == 0)
            {
                down = true;
            }
 
            // If direction is down,
            // increment, else decrement
            if (down)
            {
                row++;
            }
            else
            {
                row--;
            }
        }
 
        // Print concatenation
        // of all rows
        for (int i = 0; i < n; ++i)
        {
            System.out.print(arr[i]);
        }
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        String str = "GEEKSFORGEEKS";
        int n = 3;
        printZigZagConcat(str, n);
    }
}
 
// This code is contributed by PrinciRaj1992


Python3
# Python 3 program to print
# string obtained by
# concatenation of different
# rows of Zig-Zag fashion
 
# Prints concatenation of all
# rows of str's Zig-Zag fashion
def printZigZagConcat(str, n):
     
    # Corner Case (Only one row)
    if n == 1:
        print(str)    
        return
 
    # Find length of string
    l = len(str)
 
    # Create an array of
    # strings for all n rows
    arr=["" for x in range(l)]
 
    # Initialize index for
    # array of strings arr[]
    row = 0
     
    # Traverse through
    # given string
    for i in range(l):
         
        # append current character
        # to current row
        arr[row] += str[i]
 
        # If last row is reached,
        # change direction to 'up'
        if row == n - 1:
            down = False
 
        # If 1st row is reached,
        # change direction to 'down'
        elif row == 0:
            down = True
 
        # If direction is down,
        # increment, else decrement
        if down:
            row += 1
        else:
            row -= 1
 
    # Print concatenation
    # of all rows
    for i in range(n):
        print(arr[i], end = "")
 
# Driver Code
str = "GEEKSFORGEEKS"
n = 3
printZigZagConcat(str, n)
 
# This code is contributed
# by ChitraNayal


C#
// C# program to print string
// obtained by concatenation
// of different rows of
// Zig-Zag fashion
using System;
 
class GFG
{
     
    // Prints concatenation
    // of all rows of str's
    // Zig-Zag fashion
    static void printZigZagConcat(string str,
                                  int n)
    {
         
    // Corner Case (Only one row)
    if (n == 1)
    {
        Console.Write(str);    
        return;
    }
     
    char[] str1 = str.ToCharArray();
     
    // Find length of string
    int len = str.Length;
 
    // Create an array of
    // strings for all n rows
    string []arr = new string[n];
 
    // Initialize index for
    // array of strings arr[]
    int row = 0;
    bool down = true; // True if we are moving
                      // down in rows, else false
 
    // Traverse through
    // given string
    for (int i = 0; i < len; ++i)
    {
        // append current character
        // to current row
        arr[row] += (str1[i]);
 
        // If last row is reached,
        // change direction to 'up'
        if (row == n - 1)
        down = false;
 
        // If 1st row is reached,
        // change direction to 'down'
        else if (row == 0)
        down = true;
 
        // If direction is down,
        // increment, else decrement
        if(down)
        row++;
        else
        row--;
    }
 
    // Print concatenation
    // of all rows
    for (int i = 0; i < n; ++i)
        Console.Write(arr[i]);
    }
 
    // Driver Code
    public static void Main()
    {
        String str = "GEEKSFORGEEKS";
        int n = 3;
        printZigZagConcat(str, n);
    }
}
 
// This code is contributed
// by ChitraNayal


Javascript


C++
// C++ Program for above approach
#include
using namespace std;
// Function for zig-zag Concatenation
string zigZagConcat(string s, int n)
{
    // Check is n is less
    // or equal to 1
    if (n <= 1)
    {
        return s;
    }
    string result = "";
    // Iterate rowNum from 0 to n - 1
    for (int rowNum = 0; rowNum < n; rowNum++)
    {
        int i = rowNum;
        bool up = true;
        // Iterate i till s.length() - 1
        while (i < s.length())
        {
 
            result += s[i];
 
            // Check is rowNum is 0 or n - 1
            if (rowNum == 0 || rowNum == n - 1)
            {
                i += (2 * n - 2);
            }
            else
            {
                if (up)
                {
                    i += (2 * (n - rowNum) - 2);
                }
                else
                {
                    i += rowNum * 2;
                }
                up ^= true;
            }
        }
    }
    return result;
}
 
// Driver Code
int main()
{
    string str = "GEEKSFORGEEKS";
    int n = 3;
    cout<< zigZagConcat(str, n);
}
 
// This code is contributed by Mayank Tyagi


Java
// Java Program for above approach
import java.lang.*;
 
class Solution
{
     
    // Function for zig-zag Concatenation
    private static String zigZagConcat(
                                String s, int n)
    {
 
        // Check is n is less
        // or equal to 1
        if (n <= 1)
        {
            return s;
        }
 
        StringBuilder result = new
                             StringBuilder();
 
        // Iterate rowNum from 0 to n - 1
        for (int rowNum = 0; rowNum < n; rowNum++)
        {
            int i = rowNum;
            boolean up = true;
           
            // Iterate i till s.length() - 1
            while (i < s.length())
            {
 
                result = result.append(s.charAt(i));
               
                // Check is rowNum is 0 or n - 1
                if (rowNum == 0 || rowNum == n - 1)
                {
                    i += (2 * n - 2);
                }
                else
                {
                    if (up)
                    {
                        i += (2 * (n - rowNum) - 2);
                    }
                    else
                    {
                        i += rowNum * 2;
                    }
                    up ^= true;
                }
            }
        }
        return result.toString();
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        String str = "GEEKSFORGEEKS";
        int n = 3;
        System.out.println(zigZagConcat(str, n));
    }
}
 
// This code is contributed by Sakshi Sachdeva


Python3
# Python Program for above approach
 
# Function for zig-zag Concatenation
def zigZagConcat(s, n):
     
    # Check is n is less 
    # or equal to 1
    if (n <= 1):
        return s
     
    result = ""
     
    # Iterate rowNum from 0 to n - 1
    for rowNum in range(n):
        i = rowNum
        up = True
         
        # Iterate i till s.length() - 1
        while(i < len(s)):
            result += s[i]
             
            # Check is rowNum is 0 or n - 1
            if (rowNum == 0 or rowNum == n - 1):
                i += (2 * n - 2)
            else:
                if(up):
                    i += (2 * (n - rowNum) - 2)
                else:
                    i += rowNum * 2
                up ^= True
     
    return result
 
# Driver Code
str = "GEEKSFORGEEKS"
n = 3
print(zigZagConcat(str, n))
 
# This code is contributed by rag2127


C#
// C# Program for above approach
using System;
public class GFG{
 
  // Function for zig-zag Concatenation
  static string zigZagConcat( string s, int n)
  {
    // Check is n is less 
    // or equal to 1
    if (n <= 1) 
    {
      return s;
    }
    string result="";
    // Iterate rowNum from 0 to n - 1
    for (int rowNum = 0; rowNum < n; rowNum++)
    {
      int i = rowNum;
      bool up = true;
      // Iterate i till s.length() - 1
      while (i < s.Length) 
      {
 
        result += s[i];
 
        // Check is rowNum is 0 or n - 1
        if (rowNum == 0 || rowNum == n - 1) 
        {
          i += (2 * n - 2);
        }
        else
        {
          if (up) 
          {
            i += (2 * (n - rowNum) - 2);
          }
          else
          {
            i += rowNum * 2;
          }
          up ^= true;
        }
      }
    }
    return result;
  }
 
 
  // Driver Code
  static public void Main (){
    string str = "GEEKSFORGEEKS";
    int n = 3;
    Console.WriteLine(zigZagConcat(str, n));
  }
}
 
// This code is contributed by avanitrachhadiya2155


Javascript


输出
GSGSEKFREKEOE

时间复杂度: O(len),其中 len 是输入字符串的长度。
辅助空间: O(len)
感谢 Gaurav Ahirwar 提出上述解决方案。

另一种方法:
如果我们假设我们正在逐一遍历行数为 n 的假想矩阵并打印字符
对于第一行和最后一行,索引将增加 i+= 2*(n-1) 的值
对于中间行,如果我们向上方向,索引将增加 i+= 2*(n-rowNum-1),对于向下方向,索引将增加 2*rowNum。以下是工作实施。在Java中

下面是上述方法的实现:

C++

// C++ Program for above approach
#include
using namespace std;
// Function for zig-zag Concatenation
string zigZagConcat(string s, int n)
{
    // Check is n is less
    // or equal to 1
    if (n <= 1)
    {
        return s;
    }
    string result = "";
    // Iterate rowNum from 0 to n - 1
    for (int rowNum = 0; rowNum < n; rowNum++)
    {
        int i = rowNum;
        bool up = true;
        // Iterate i till s.length() - 1
        while (i < s.length())
        {
 
            result += s[i];
 
            // Check is rowNum is 0 or n - 1
            if (rowNum == 0 || rowNum == n - 1)
            {
                i += (2 * n - 2);
            }
            else
            {
                if (up)
                {
                    i += (2 * (n - rowNum) - 2);
                }
                else
                {
                    i += rowNum * 2;
                }
                up ^= true;
            }
        }
    }
    return result;
}
 
// Driver Code
int main()
{
    string str = "GEEKSFORGEEKS";
    int n = 3;
    cout<< zigZagConcat(str, n);
}
 
// This code is contributed by Mayank Tyagi

Java

// Java Program for above approach
import java.lang.*;
 
class Solution
{
     
    // Function for zig-zag Concatenation
    private static String zigZagConcat(
                                String s, int n)
    {
 
        // Check is n is less
        // or equal to 1
        if (n <= 1)
        {
            return s;
        }
 
        StringBuilder result = new
                             StringBuilder();
 
        // Iterate rowNum from 0 to n - 1
        for (int rowNum = 0; rowNum < n; rowNum++)
        {
            int i = rowNum;
            boolean up = true;
           
            // Iterate i till s.length() - 1
            while (i < s.length())
            {
 
                result = result.append(s.charAt(i));
               
                // Check is rowNum is 0 or n - 1
                if (rowNum == 0 || rowNum == n - 1)
                {
                    i += (2 * n - 2);
                }
                else
                {
                    if (up)
                    {
                        i += (2 * (n - rowNum) - 2);
                    }
                    else
                    {
                        i += rowNum * 2;
                    }
                    up ^= true;
                }
            }
        }
        return result.toString();
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        String str = "GEEKSFORGEEKS";
        int n = 3;
        System.out.println(zigZagConcat(str, n));
    }
}
 
// This code is contributed by Sakshi Sachdeva

Python3

# Python Program for above approach
 
# Function for zig-zag Concatenation
def zigZagConcat(s, n):
     
    # Check is n is less 
    # or equal to 1
    if (n <= 1):
        return s
     
    result = ""
     
    # Iterate rowNum from 0 to n - 1
    for rowNum in range(n):
        i = rowNum
        up = True
         
        # Iterate i till s.length() - 1
        while(i < len(s)):
            result += s[i]
             
            # Check is rowNum is 0 or n - 1
            if (rowNum == 0 or rowNum == n - 1):
                i += (2 * n - 2)
            else:
                if(up):
                    i += (2 * (n - rowNum) - 2)
                else:
                    i += rowNum * 2
                up ^= True
     
    return result
 
# Driver Code
str = "GEEKSFORGEEKS"
n = 3
print(zigZagConcat(str, n))
 
# This code is contributed by rag2127

C#

// C# Program for above approach
using System;
public class GFG{
 
  // Function for zig-zag Concatenation
  static string zigZagConcat( string s, int n)
  {
    // Check is n is less 
    // or equal to 1
    if (n <= 1) 
    {
      return s;
    }
    string result="";
    // Iterate rowNum from 0 to n - 1
    for (int rowNum = 0; rowNum < n; rowNum++)
    {
      int i = rowNum;
      bool up = true;
      // Iterate i till s.length() - 1
      while (i < s.Length) 
      {
 
        result += s[i];
 
        // Check is rowNum is 0 or n - 1
        if (rowNum == 0 || rowNum == n - 1) 
        {
          i += (2 * n - 2);
        }
        else
        {
          if (up) 
          {
            i += (2 * (n - rowNum) - 2);
          }
          else
          {
            i += rowNum * 2;
          }
          up ^= true;
        }
      }
    }
    return result;
  }
 
 
  // Driver Code
  static public void Main (){
    string str = "GEEKSFORGEEKS";
    int n = 3;
    Console.WriteLine(zigZagConcat(str, n));
  }
}
 
// This code is contributed by avanitrachhadiya2155

Javascript


输出
GSGSEKFREKEOE

时间复杂度: O(长度)

空间复杂度: O(1)