aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorterminaldweller <thabogre@gmail.com>2022-04-23 18:31:34 +0000
committerterminaldweller <thabogre@gmail.com>2022-04-23 18:31:34 +0000
commit4cea950dc25c0470262b591f22c758b1be07abce (patch)
treebf76f0cc151bd837844f35c42cb5147bbb0ce6f0
parentupdate (diff)
downloadleetcode-4cea950dc25c0470262b591f22c758b1be07abce.tar.gz
leetcode-4cea950dc25c0470262b591f22c758b1be07abce.zip
update
Diffstat (limited to '')
-rw-r--r--535/main.go48
1 files changed, 48 insertions, 0 deletions
diff --git a/535/main.go b/535/main.go
new file mode 100644
index 0000000..425f9d5
--- /dev/null
+++ b/535/main.go
@@ -0,0 +1,48 @@
+package main
+
+import (
+ "crypto/sha256"
+ "fmt"
+)
+
+type Codec struct {
+ table map[string]string
+}
+
+func Constructor() Codec {
+ var codec Codec
+ table := make(map[string]string)
+ codec.table = table
+ return codec
+}
+
+// Encodes a URL to a shortened URL.
+func (this *Codec) encode(longUrl string) string {
+ hash := sha256.Sum256([]byte(longUrl))
+ hashStr := string(hash[:])
+ tinyUrl := "http://tinyurl.com/" + hashStr
+ this.table[hashStr] = longUrl
+ return tinyUrl
+}
+
+// Decodes a shortened URL to its original URL.
+func (this *Codec) decode(shortUrl string) string {
+ return this.table[shortUrl[19:]]
+
+}
+
+/**
+ * Your Codec object will be instantiated and called as such:
+ * obj := Constructor();
+ * url := obj.encode(longUrl);
+ * ans := obj.decode(url);
+ */
+
+func main() {
+ longUrl := "https://leetcode.com/problems/design-tinyurl"
+ obj := Constructor()
+ url := obj.encode(longUrl)
+ fmt.Println(url)
+ ans := obj.decode(url)
+ fmt.Println(ans)
+}