This page is still under construction.

Parts of this page are still being built. What you see may change.

git mv

Interview

Time limit1sMemory limit1024 MB

Summary
Given two Unix paths, output the shortest string of the form A{B => C}D that rewrites the source path into the destination path.
Level

Medium6 of 10

Topics
String, Implementation, Greedy, Two pointers
Solved
No attempts yet

Problem

During development, you recently moved a file from one location to another. To keep your development team up to date with the change you made, you want to send them a short description of the change, without making use of any versioning software.

Both the source location and destination are valid Unix path names, that is, a nonempty string consisting of lowercase letters and "/" such that no "/" occurs at the beginning or the end, nor does it contain two consecutive forward slashes.

You need to find the shortest string of the form "A{B => C}D" such that:

  • The source location is "ABD" and the destination is "ACD", where double forward slashes should be read as one forward slash. For example, if a file is moved from "a/c" to "a/b/c", we can describe this movement by "a/{ => b}/c", meaning the source location was "a/c" and not "a//c".
  • The string AA is empty or ends with a forward slash, and similarly DD is empty or starts with a forward slash.
  • Both BB and CC do not start or end with a forward slash.

Input

The input consists of:

  • One line containing the source location.
  • One line containing the destination location.

Both lines will contain at most 10610^6 characters, will not begin or end with a forward slash and will not contain any directory name twice. The two strings are guaranteed to be different.

Output

Output the shortest replacement string that transforms the source location to the destination, satisfying the above constraints.

Examples2

  1. Example 1

    Input
    www/public/passwords
    private/passwords
    
    Expected output
    {www/public => private}/passwords
    
  2. Example 2

    Input
    home/linus/downloads/image
    home/linus/pictures/recent/image
    
    Expected output
    home/linus/{downloads => pictures/recent}/image