Four lines,boring solution

  • 0

    remove ":/" and add ":/" to shorten and enlarge

    public class Codec {
        // Encodes a URL to a shortened URL.
        public String encode(String longUrl) {
            int i = longUrl.indexOf(":");
            return longUrl.substring(0,i)+longUrl.substring(i+2);
        // Decodes a shortened URL to its original URL.
        public String decode(String shortUrl) {
            int i = shortUrl.indexOf("/");
            return shortUrl.substring(0,i)+ ":/"+shortUrl.substring(i);

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.