📜  通过填充元素将给定的字符串拆分为大小为 K 的子字符串

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

通过填充元素将给定的字符串拆分为大小为 K 的子字符串

给定一个长度为N的字符串str和一个整数K ,任务是将字符串分成K个大小的组,如果最后一组没有剩余K个字符,则使用字符ch来完成该组。

例子:

方法:这是一个简单的实现相关问题。请按照以下步骤操作:

  1. res初始化为空字符串。
  2. 开始遍历字符串,当res字符串的大小等于K 时,然后将res字符串放入结果向量中,并再次清空res字符串以进行进一步划分
  3. 最后,如果res字符串不为空且 size 不等于 ka,则使用 Extra字符填充它,最后一组。

下面是上述方法的实现。

C++
// C++ code to implement above approach
#include 
using namespace std;
 
// Function to split the string
vector dividestring(string str,
                            int K, char ch)
{
    int N = str.size();
    int j = 0, i = 0;
    vector result;
    string res = "";
    while (j < N) {
        res += str[j];
        if (res.size() == K) {
            result.push_back(res);
            res = "";
        }
        j++;
    }
 
    if (res != "") {
        while (res.size() < K) {
            res += ch;
        }
        result.push_back(res);
    }
    return result;
}
 
// Driver code
int main()
{
 
    string str = "Algorithms";
    int K = 3;
    char ch = '@';
    vector ans
        = dividestring(str, K, ch);
    for (auto i : ans) {
        cout << i << "\n";
    }
    return 0;
}


Java
// Java code to implement above approach
import java.util.ArrayList;
 
class GFG
{
 
  // Function to split the String
  static ArrayList divideString(String str, int K, char ch) {
    int N = str.length();
    int j = 0;
    ArrayList result = new ArrayList();
    String res = "";
    while (j < N) {
      res += str.charAt(j);
      if (res.length() == K) {
        result.add(res);
        res = "";
      }
      j++;
    }
 
    if (res != "") {
      while (res.length() < K) {
        res += ch;
      }
      result.add(res);
    }
    return result;
  }
 
  // Driver code
  public static void main(String args[])
  {
 
    String str = "Algorithms";
    int K = 3;
    char ch = '@';
    ArrayList ans = divideString(str, K, ch);
    for (String i : ans) {
      System.out.println(i);
    }
  }
}
 
// This code is contributed by gfgking.


Python3
# python3 code to implement above approach
 
# Function to split the string
 
 
def dividestring(str, K, ch):
 
    N = len(str)
    j, i = 0, 0
    result = []
    res = ""
    while (j < N):
        res += str[j]
        if (len(res) == K):
            result.append(res)
            res = ""
 
        j += 1
 
    if (res != ""):
        while (len(res) < K):
            res += ch
 
        result.append(res)
 
    return result
 
 
# Driver code
if __name__ == "__main__":
 
    str = "Algorithms"
    K = 3
    ch = '@'
    ans = dividestring(str, K, ch)
    for i in ans:
        print(i)
 
    # This code is contributed by rakeshsahni


C#
// C# code to implement above approach
using System;
using System.Collections.Generic;
class GFG {
 
  // Function to split the string
  static List dividestring(string str, int K,
                                   char ch)
  {
    int N = str.Length;
    int j = 0;
    List result = new List();
    string res = "";
    while (j < N) {
      res += str[j];
      if (res.Length == K) {
        result.Add(res);
        res = "";
      }
      j++;
    }
 
    if (res != "") {
      while (res.Length < K) {
        res += ch;
      }
      result.Add(res);
    }
    return result;
  }
 
  // Driver code
  public static void Main()
  {
 
    string str = "Algorithms";
    int K = 3;
    char ch = '@';
    List ans = new List();
    ans = dividestring(str, K, ch);
    foreach(var i in ans) { Console.WriteLine(i); }
  }
}
 
// This code is contributed by Taranpreet


Javascript



输出:
Alg
ori
thm
s@@

时间复杂度: O(N)
辅助空间: O(N)