aboutsummaryrefslogtreecommitdiff
path: root/encode_decode_tinyurl.c
diff options
context:
space:
mode:
Diffstat (limited to 'encode_decode_tinyurl.c')
-rw-r--r--encode_decode_tinyurl.c31
1 files changed, 31 insertions, 0 deletions
diff --git a/encode_decode_tinyurl.c b/encode_decode_tinyurl.c
new file mode 100644
index 0000000..2c7ddf7
--- /dev/null
+++ b/encode_decode_tinyurl.c
@@ -0,0 +1,31 @@
+// 535. Encode and Decode TinyURL
+#include "leetcode.h"
+
+/*
+ * tinyurl is a url shortening service where you enter a url such as
+ * https://looooooongurl.com and it returns a short url such as
+ * http://tinyurl.com/4e9iAk. design a class to encode a url and decode a tiny
+ * url. there is no restriction on how your encode/decode algorithm should work.
+ * you just need to ensure that a url can be encoded to a tiny url and the tiny
+ * url can be decoded to the original url.
+ */
+
+char *encode(char *longUrl) {
+ char *e = longUrl + strlen(longUrl) - 1,
+ *tiny = (char *)calloc(10000, sizeof(char));
+ while (*e-- != '/')
+ ;
+ strcat(strcpy(tiny, "https://tinyurl.com/"), e + 2);
+ strcpy(tiny + strlen(tiny) + 1, longUrl);
+ return tiny;
+}
+
+char *decode(char *shortUrl) { return shortUrl + strlen(shortUrl) + 1; }
+
+int main() {
+ char *url = "https://leetcode.com/problems/design-tinyurl";
+ char *enc = encode(url);
+ printf("%s\n",
+ decode(enc)); // expect: https://leetcode.com/problems/design-tinyurl
+ free(enc);
+}