Problem Β· String

URL / Path Segment Compression Part 1

Learn this problem
● EasyStripe logoStripeFULLTIMEONSITE INTERVIEW
See Stripe hiring insights

Problem statement

You are given a string built from major parts separated by /, where each major part is built from minor parts separated by . β€” for example abcd/erfgsh/google.com.abc.

Compress every minor part with this rule: keep its first and last character and replace the middle with the count of removed characters. Formally a minor part w becomes w[0] + (len(w) - 2) + w[last]. Then rejoin minor parts with . and major parts with /.

For example abcd becomes a2d (drop bc, count 2), and google becomes g4e. Append the count digit-by-digit, so a long segment like googlecomabc (length 12) becomes g10c β€” the middle count can be multiple digits.

Assume every minor part has length >= 2, and that there are no empty segments (no leading / and no consecutive ..).

Function

compress(s: String) β†’ String

Examples

Example 1

s = "abcd/erfgsh/google.com.abc"return = "a2d/e4h/g4e.c1m.a1c"
abcd -> a2d (drop bc). erfgsh -> e4h (drop rfgs, count 4). google -> g4e, com -> c1m, abc -> a1c; the third major rejoins its minors with dots: g4e.c1m.a1c.

Example 2

s = "googlecomabc"return = "g10c"
A single segment of length 12 keeps the first char g and last char c, with the middle count 10 appended digit-by-digit, giving g10c. The multi-digit count is the common trap.

Constraints

  • Major parts are separated by /; within a major, minor parts are separated by ..
  • Every minor part has length >= 2.
  • A minor part w compresses to w[0] + (len(w) - 2) + w[last], with the count written digit-by-digit.
  • No empty segments (no leading /, no consecutive ..).

More Stripe problems

drafts saved locally
public String compress(String s) {
  // write your code here
}
s"abcd/erfgsh/google.com.abc"
expected"a2d/e4h/g4e.c1m.a1c"
checking account