Problem Β· String

URL Segment Compression Part 2 β€” Bounded Minor Parts

Learn this problem
● MediumStripe logoStripeFULLTIMEONSITE INTERVIEW
See Stripe hiring insights

Problem statement

A path is split into major parts by /. Each major part is split into minor parts by .. Base-compress a word w as its first character, the number of omitted middle characters, and its last character.

For each major part independently:

  • If it has at most m minor parts, base-compress every minor part exactly as in Part 1.
  • If it has more than m minor parts, base-compress the first m-1 parts separately. Concatenate the original characters of all remaining minor parts (drop their dots) and base-compress that concatenated word as the final token.

The merge uses original characters, never already-compressed tokens. Rejoin minor tokens with . and major parts with /.

Practice sequence

  1. Part 1: compress every minor part
  2. Part 2: cap each major at m minor tokens (current)

Function

compressBounded(s: String, m: int) β†’ String

Examples

Example 1

s = "stripe.com/payments/checkout/customer.john.doe"m = 2return = "s4e.c1m/p6s/c6t/c6r.j5e"
customer is compressed separately; john and doe are concatenated to johndoe and compressed as j5e. One-minor majors such as payments still become p6s.

Example 2

s = "www.api.stripe.com/checkout"m = 3return = "w1w.a1i.s7m/c6t"
The first two minor parts stay separately compressed. stripe and com are merged from original text to stripecom, producing s7m. checkout remains c6t.

Constraints

  • m >= 1.
  • The input has no empty major or minor parts.
  • Every minor part has length at least 2.
  • The middle count may contain multiple digits.
  • A one-minor major is base-compressed just as in Part 1.

More Stripe problems

drafts saved locally
public String compressBounded(String s, int m) {
  // write your code here
}
s"stripe.com/payments/checkout/customer.john.doe"
m2
expected"s4e.c1m/p6s/c6t/c6r.j5e"
checking account