Skip to content

Commit

Permalink
Create 0535-Encode-and-Decode-TinyURL.Swift
Browse files Browse the repository at this point in the history
  • Loading branch information
kabirdhillon7 committed Jan 20, 2023
1 parent 61fa9b4 commit f6ed97e
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions swift/0535-Encode-and-Decode-TinyURL.Swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class Codec {
var urlMap = [String: String]()

// Encodes a URL to a shortened URL.
func encode(_ longUrl: String) -> String {
var id = UUID().uuidString

if urlMap[id] != nil {
id = UUID().uuidString
}

urlMap[id] = longUrl
return "http://tinyurl.com/\(id)"
}

// Decodes a shortened URL to its original URL.
func decode(_ shortUrl: String) -> String {
let id = shortUrl.split(separator:"/").last!
return urlMap[String(id)]!
}
}

/**
* Your Codec object will be instantiated and called as such:
* let obj = Codec()
* val s = obj.encode(longUrl)
* let ans = obj.decode(s)
*/

0 comments on commit f6ed97e

Please sign in to comment.