Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #349 (“Power String” and “Meetings Point”)

For those not familiar with "The Weekly Challenge", it is a weekly programming puzzle with two parts, with a new pair of tasks each Monday. You can find it here: The Weekly Challenge

The Weekly Challenge for the week of 2025-11-24 through 2025-11-30 is #349. The tasks for challenge #349 are as follows:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 349-1: Power String
Submitted by: Mohammad Sajid Anwar
You are given a string. Write a script to return the power of
the given string. The power of the string is the maximum length
of a non-empty substring that contains only one unique character.

Example #1:
Input: $str = "textbook"
Output: 2
Breakdown: "t", "e", "x", "b", "oo", "k"
The longest substring with one unique character is "oo".

Example #2:
Input: $str = "aaaaa"
Output: 5

Example #3:
Input: $str = "hoorayyy"
Output: 3
Breakdown: "h", "oo", "r", "a", "yyy"
The longest substring with one unique character is "yyy".

Example #4:
Input: $str = "x"
Output: 1

Example #5:
Input: $str = "aabcccddeeffffghijjk"
Output: 4
Breakdown: "aa", "b", "ccc", "dd", "ee", "ffff", "g", "h", "i",
           "jj", "k"
The longest substring with one unique character is "ffff".

The use of the word "unique" is a bit ambiguous here. Depending on how that's defined, the "power" of the string "sjfhrrsiggghhhh" would be 3 if "hhhh" is rejected as "not being unique" seeing as how "h" has been previously seen. But if "unique" means "within one substring", then the "power" is 4. I'll assume the latter interpretation, as the examples allow either. Using that interpretation, this is just a matter of counting repeats in a variable $r and keeping track of "maximum repeats seen so far" in a variable $max.

Robbie Hatley's Perl Solution to The Weekly Challenge 349-1

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 349-2: Meeting Point
Submitted by: Mohammad Sajid Anwar
You are given instruction string made up of U (up), D (down),
L (left) and R (right). Write a script to return true if
following the instruction, you meet (0,0) at any point along
the sequence.

Example #1:
Input: $path = "ULD"
Output: false
(-1,1) <- (0,1)
   |        ^
   v        |
(-1,0)    (0,0)

Example #2:
Input: $path = "ULDR"
Output: true
 (-1,1) <- (0,1)
    |        ^
    v        |
 (-1,0) -> (0,0)

Example #3:
Input: $path = "UUURRRDDD"
Output: false
(0,3) -> (1,3) -> (2,3) -> (3,3)
  ^                          |
  |                          v
(0,2)                      (3,2)
  ^                          |
  |                          v
(0,1)                      (3,1)
  ^                          |
  |                          v
(0,0)                      (3,0)

Example #4:
Input: $path = "UURRRDDLLL"
Output: true
(0,2) -> (1,2) -> (2,2) -> (3,2)
  ^                          |
  |                          v
(0,1)                      (3,1)
  ^                          |
  |                          v
(0,0) <- (1,0) <- (1,1) <- (3,0)

Example #5:
Input: $path = "RRUULLDDRRUU"
Output: true
(0,2) <- (1,2) <- (2,2)
  |                 ^
  v                 |
(0,1)             (2,1)
  |                 ^
  v                 |
(0,0) -> (1,0) -> (2,1)

Assuming that we start at (0,0), this problem is just a matter of seeing if any future point is (0,0), and if it is, we can return 'true' immediately. If we never see (0,0) again after leaving it, then return 'false'.

One might be tempted to take this shortcut: Simply compare Us to Ds and Ls to Rs and return 'true' if equal, 'false' if not. This, however, is a mistake, because it only determines if the END point is (0,0), whereas we want to determine if ANY point other than the first is (0,0).

Robbie Hatley's Perl Solution to The Weekly Challenge 349-2

That's it for challenge 349; see you on challenge 350!

Comments

Popular posts from this blog

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #334 (“Range Sum” and “Nearest Valid Point”)

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #336 (“Equal Group” and “Final Score”)

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #326 (“Day of Year” and “Decompressed List”)