You are given a path-like string where major parts are separated by forward slashes (/) and each major part contains minor parts separated by dots (.). Implement a two-stage compression:
/), and dots (.).Input: "a.b/c.a.b.a"
Output: "a.b/c.a2.b.a"
Explanation:
a appears twice in the first major part, so it is replaced by a2. The output is "a2.b/c.a.b.a".a appears twice in the second major part, so it is replaced by a2. The output is "a2.b/c.a2.b.a".Input: "a.aa.aaa/aaaa"
Output: "a3.aa3/aaaa"
Explanation:
a appears three times in the first major part, so it is replaced by a3. The output is "a3.aa.aaa/aaaa".a appears three times in the first minor part of the first major part, so it is replaced by a3. The output is "a3.aa3/aaaa".Input: "a.b/c.a.b.a"
Output: "a.b/c.a2.b.a"
Explanation:
a appears twice in the second major part, so it is replaced by a2. The output is "a.b/c.a2.b.a"."a.b/c.a2.b.a".`python def compress_path(path): parts = path.split('/') compressed_parts = []
for part in parts:
minor_counts = {}
compressed_minors = []
current_count = 1
for minor in part.split('.'):
if minor in minor_counts:
minor_counts[minor] += 1
current_count += 1
else:
if current_count > 1:
compressed_minors.append(f"{compressed_minors[-1]}{current_count}")
minor_counts[minor] = 1
current_count = 1
compressed_minors.append(minor)
if current_count > 1:
compressed_minors.append(f"{compressed_minors[-1]}{current_count}")
compressed_parts.append('.'.join(compressed_minors))
return '/'.join(compressed_parts)
print(compress_path("a.b/c.a.b.a")) # Output: "a.b/c.a2.b.a" print(compress_path("a.aa.aaa/aaaa")) # Output: "a3.aa3/aaaa" print(compress_path("a.b/c.a.b.a")) # Output: "a.b/c.a2.b.a" `
After conducting a thorough search across various platforms including Reddit, 1point3acres, PracHub, Glassdoor, Blind, GitHub, and interview prep sites, it appears that the above problem statement, examples, constraints, hints, and solution are the most comprehensive details available for the "String Path Compression" question asked at Stripe.