Shorten URL pattern

Difficulty: MEDIUMCategory: DSAFrequency: 1Asked On 2025-06-02

Problem Statement

# Given a String, split it into major parts separated by special char '/'.

# For each major part that’s split by '/', we can further split it into minor parts separated by '.'.

# ### Example 1

# str = stripe.com/payments/checkout/customer.john.doe

# minor_parts = 2

# after Part 1 compression

# =>

# s4e.c1m/p6s/c6t/c6r.j2n.d1e

# after Part 2 compression

# =>

# s4e.c1m/p6s/c6t/c6r.j5e

# # ### Example 2

# Given:

# str = www.api.stripe.com/checkout

# minor_parts = 3

# (afte‍‌‌‌‍‍‍‍‌‍‌‌‍‍‍‍‍‍‌‌r Part 1 compression)

# =>

# w1w.a1i.s4e.c1m/c6t

# (then after Part 2 compression)

# =>

# w1w.a1i.s7m/c6t

First part: shorten every minor_part

Second part: you get another parameter m. Every major_part can only contain m minor_part

Third part: the entire compressed_url can only contain m minor_part, some posts said you are given m and t where m is the number of minor_part you can have in a major_part and t is the total number of minor_part you are allowed to have

Solution

Comments