PHP - Divide a String Into Groups of Size k - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.

Name: PHP - Divide a String Into Groups of Size k fullscreencopydownloadembedprint


Your result can be seen below.

Result of php executing





Full code of PHP - Divide a String Into Groups of Size k.php

  1. <?php
  2.  
  3.         /**
  4.         *Divide a String Into Groups of Size k
  5.         *A string s can be partitioned into groups of size k using the following procedure:
  6.  
  7.     *The first group consists of the first k characters of the string, the second group consists of the next k characters of the string, and so on. Each character can be a part of exactly one group.
  8.     *For the last group, if the string does not have k characters remaining, a character fill is used to complete the group.
  9.     *Note that the partition is done so that after removing the fill character from the last group (if it exists) and concatenating all the groups in order, the resultant string should be s.
  10.  
  11.     *Given the string s, the size of each group k and the character fill, return a string array denoting the composition of every group s has been divided into, using the above procedure.
  12.  
  13.         *Example 1:
  14.  
  15.     *Input: s = "abcdefghi", k = 3, fill = "x"
  16.     *Output: ["abc","def","ghi"]
  17.     *Explanation:
  18.     *The first 3 characters "abc" form the first group.
  19.     *The next 3 characters "def" form the second group.
  20.     *The last 3 characters "ghi" form the third group.
  21.     *Since all groups can be completely filled by characters from the string, we do not need to use fill.
  22.     *Thus, the groups formed are "abc", "def", and "ghi".
  23.  
  24.  
  25.         *Example 2:
  26.  
  27.     *Input: s = "abcdefghij", k = 3, fill = "x"
  28.     *Output: ["abc","def","ghi","jxx"]
  29.     *Explanation:
  30.     *Similar to the previous example, we are forming the first three groups "abc", "def", and "ghi".
  31.     *For the last group, we can only use the character 'j' from the string. To complete this group, we add 'x' twice.
  32.     *Thus, the 4 groups formed are "abc", "def", "ghi", and "jxx".
  33.  
  34.  
  35.     /**
  36.      * @param String $s
  37.      * @param Integer $k
  38.      * @param String $fill
  39.      * @return String[]
  40.      */
  41.     function divideString($s, $k, $fill) {
  42.         //Your code
  43.     }
  44.     
  45.     $example1 = divideString("abcdefghi",3,"x");
  46.     $example2 = divideString("abcdefghij",3,"x");
  47.     
  48.     $test = divideString("phptest",5,"Y");
  49.     
  50.     echo json_encode(($test));
  51. ?>
File Description
  • PHP - Divide a String Into Groups of Size k
  • PHP Code
  • 17 Jan-2022
  • 2.02 Kb
You can Share it: