Skip to content

Commit

Permalink
Create 0535-encode-and-decode-tinyurl.go
Browse files Browse the repository at this point in the history
  • Loading branch information
AP-Repositories authored Jan 1, 2023
1 parent 0cb22b5 commit aeb1444
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions go/0535-encode-and-decode-tinyurl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
type Codec struct {
encodeMap map[string]string
decodeMap map[string]string
}
const base string = "http://tinyurl.com/"


func Constructor() Codec {
return Codec{make(map[string]string), make(map[string]string)}
}

// Encodes a URL to a shortened URL.
func (this *Codec) encode(longUrl string) string {
if _, ok := this.encodeMap[longUrl]; !ok {
shortUrl := base + strconv.Itoa(len(this.encodeMap) + 1)
this.encodeMap[longUrl] = shortUrl
this.decodeMap[shortUrl] = longUrl
}
return this.encodeMap[longUrl]
}

// Decodes a shortened URL to its original URL.
func (this *Codec) decode(shortUrl string) string {
return this.decodeMap[shortUrl]
}

0 comments on commit aeb1444

Please sign in to comment.