Skip to content

Instantly share code, notes, and snippets.

@manwar
Created April 28, 2026 15:35
Show Gist options
  • Select an option

  • Save manwar/d8a06e02326c669522511f0ccc3f63a0 to your computer and use it in GitHub Desktop.

Select an option

Save manwar/d8a06e02326c669522511f0ccc3f63a0 to your computer and use it in GitHub Desktop.
The Weekly Challenge - 372

The Weekly Challenge - 372

Early Bird Club members ONLY

Task 1: Rearrange Spaces

Submitted by: Mohammad Sajid Anwar

You are given a string text of words that are placed among number of spaces.

Write a script to rearrange the spaces so that there is an equal number of spaces between every pair of adjacent words and that number is maximised. If you can't distribute, place the extra speaces at the end. Finally return the string.

Example 1

Input: $str = "  challenge  "
Output: "challenge    "

We have 4 spaces and 1 word. So all spaces go to the end.

Example 2

Input: $str = "coding  is  fun"
Output: "coding  is  fun"

We have 4 spaces and 3 words (2 gaps). So 2 spaces per gap.

Example 3

Input: $str = "a b c  d"
Output: "a b c d "

We have 4 spaces and 4 words (3 gaps). So 1 space per gap and 1 remainder.

Example 4

Input: $str = "  team      pwc  "
Output: "team          pwc"

We have 10 spaces and 2 words (1 gap). So 10 spaces per gap.

Example 5

Input: $str = "   the  weekly  challenge  "
Output: "the    weekly    challenge "

We have 9 spaces and 3 words (2 gaps). So 4 spaces per gap and 1 remainder.

Task: Largest Substring

Submitted by: Mohammad Sajid Anwar

You are given a string.

Write a script to return the length of the largest substring between two equal characters excluding the two characters. Return -1 if there is no such substring.

Example 1

Input: $str = "aaaaa"
Output: 3

For character "a", we have substring "aaa".

Example 2

Input: $str = "abcdeba"
Output: 5

For character "a", we have substring "bcdeb".

Example 3

Input: $str = "abbc
Output: 0

For character "b", we have substring "".

Example 4

Input: $str = "abcaacbc"
Output: 4

For character "a", we have substring "bca".
For character "b", we have substring "caac".
For character "c", we have substring "aacb".

Example 5

Input: $str = "laptop"
Output: 2

For character "p", we have substring "to".

Last date to submit the solution 23:59 (UK Time) Sunday 10th May 2026.


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment