diff --git a/README.md b/README.md index fb3d63d..dbfa463 100644 --- a/README.md +++ b/README.md @@ -1,39 +1,39 @@ -[![Blog Cover Image](./static/bovem-cover.png)](https://www.bovem.in/) +[![Blog Cover Image](./static/bovem-cover.png)](https://www.avni.sh/) Blogs on Self-Hosting, Homelab and DevOps Technologies. ## Index -- Kubernetes - - Containers - - Container Architecture - - Container Lifecycle - - Container Images - - Building Container Images - - Kubernetes Operators - - Operators on OpenShift - - Operator SDK and Bundle Images - - Helm Charts - - Container Network Interfaces (CNI) - - Container Storage Interfaces (CSI) - - Network Functions -- Homelab - - Building Your Own Homelab -- Go - - Go Programming Language - - File Handling in Go - - Concurrency in Go - - REST API Requests in Go -- Data Structures and Algorithms - - Time Complexity - - Arrays, Strings, and HashMaps - - Rabin-Karp Substring Search - - Checking an Array for Duplicate Values - - Identifying Anagrams - - Finding Elements in an Array that Sum Up to a Target Value - - Group Anagrams in an Array - - Finding Most Frequent Elements in an Array - - Building a Product Array without the Element Itself - - Checking Validity of a Sudoku Grid +- Kubernetes + - Containers + - Container Architecture + - Container Lifecycle + - Container Images + - Building Container Images + - Kubernetes Operators + - Operators on OpenShift + - Operator SDK and Bundle Images + - Helm Charts + - Container Network Interfaces (CNI) + - Container Storage Interfaces (CSI) + - Network Functions +- Homelab + - Building Your Own Homelab +- Go + - Go Programming Language + - File Handling in Go + - Concurrency in Go + - REST API Requests in Go +- Data Structures and Algorithms + - Time Complexity + - Arrays, Strings, and HashMaps + - Rabin-Karp Substring Search + - Checking an Array for Duplicate Values + - Identifying Anagrams + - Finding Elements in an Array that Sum Up to a Target Value + - Group Anagrams in an Array + - Finding Most Frequent Elements in an Array + - Building a Product Array without the Element Itself + - Checking Validity of a Sudoku Grid ## Technologies Used - [Hugo Static Site Generator](https://gohugo.io/) diff --git a/config.yml b/config.yml index 7f7ce03..3f1e5e6 100644 --- a/config.yml +++ b/config.yml @@ -1,5 +1,5 @@ -baseURL: "https://www.bovem.in" -title: "bovem" +baseURL: "https://www.avni.sh" +title: "avni.sh" paginate: 5 theme: PaperMod @@ -68,7 +68,7 @@ params: ShowRssButtonInSectionTermList: true ShowToc: true # comments: false - images: ["bovem-cover.png"] + images: ["avnish-cover.png"] profileMode: enabled: false diff --git a/content/contents.md b/content/contents.md index 1ae67e5..5bc7be5 100644 --- a/content/contents.md +++ b/content/contents.md @@ -36,3 +36,4 @@ summary: "Index of all content" - Finding Most Frequent Elements in an Array - Building a Product Array without the Element Itself - Checking Validity of a Sudoku Grid + - Encoding and Decoding Functions for Strings diff --git a/content/posts/dsa/encoding-and-decoding-strings/encoding-and-decoding-strings-brute-force.png b/content/posts/dsa/encoding-and-decoding-strings/encoding-and-decoding-strings-brute-force.png new file mode 100644 index 0000000..623ebf5 Binary files /dev/null and b/content/posts/dsa/encoding-and-decoding-strings/encoding-and-decoding-strings-brute-force.png differ diff --git a/content/posts/dsa/encoding-and-decoding-strings/encoding-and-decoding-strings-cover.png b/content/posts/dsa/encoding-and-decoding-strings/encoding-and-decoding-strings-cover.png new file mode 100644 index 0000000..d2b8aca Binary files /dev/null and b/content/posts/dsa/encoding-and-decoding-strings/encoding-and-decoding-strings-cover.png differ diff --git a/content/posts/dsa/encoding-and-decoding-strings/encoding-and-decoding-strings-optimized.png b/content/posts/dsa/encoding-and-decoding-strings/encoding-and-decoding-strings-optimized.png new file mode 100644 index 0000000..5ab79df Binary files /dev/null and b/content/posts/dsa/encoding-and-decoding-strings/encoding-and-decoding-strings-optimized.png differ diff --git a/content/posts/dsa/encoding-and-decoding-strings/encoding-and-decoding-strings-problem.png b/content/posts/dsa/encoding-and-decoding-strings/encoding-and-decoding-strings-problem.png new file mode 100644 index 0000000..c7453f8 Binary files /dev/null and b/content/posts/dsa/encoding-and-decoding-strings/encoding-and-decoding-strings-problem.png differ diff --git a/content/posts/dsa/encoding-and-decoding-strings/index.md b/content/posts/dsa/encoding-and-decoding-strings/index.md new file mode 100644 index 0000000..422c321 --- /dev/null +++ b/content/posts/dsa/encoding-and-decoding-strings/index.md @@ -0,0 +1,338 @@ +--- +author: "Avnish" +title: "Encoding and Decoding Functions for Strings" +date: "2023-11-06" +description: "Implement an encode function to encode a list of string values to be transmitted over a network. Also, implement a decode function to convert the encoded string back to its original value." +tags: ["data-structures", "strings", "go", "neetcode-150", "leetcode-medium"] +categories: ["Data Structures"] +series: ["Data Structures and Algorithms"] +aliases: + ["encoding-and-decoding-functions-for-strings", "encoding-and-decoding-strings"] +ShowToc: true +TocOpen: false +comments: false +cover: + image: "encoding-and-decoding-strings-cover.png" + linkFullImages: true + alt: "The encode function will encode a list of strings to a single string while the decode function will take the encoded string and return the original list of strings as output" + caption: "" + relative: false + responsiveImages: false +math: true +--- + +# Problem Statement +We have to implement an `encode` function that takes a list of string values as input and returns a single encoded string that could be transmitted over a network. On the other side of the network, the `decode` function will take the encoded string as input and return the original list of string values as output. + +

Problem statement for encoding and decoding strings

+ +A delimiter could be used to differentiate between words. For example, `["Hello", "World"]` could be encoded with comma delimiter (`,`) to `"Hello,World"`. + +# Brute Force Solution +In the brute-force approach to solving this problem, we can execute a loop over the input list appending each string with some special character as a delimiter. + +

Brute-force solution for encoding and decoding strings

+ +If the special character is a part of the string itself (for example `"Hello,"`) then we can add an escape character before it, such that it is easier to differentiate from the delimiter during decoding. + +## Psuedo-code for the Brute Force Solution +```text +func encode(array) + delimiter = "," + encodedString = "" + + loop index on array + string = array[index] + new_string = "" + + loop index2 on string + + if string[index2] is delimiter or "\" + new_string.append("\") + + new_string.append(string[index2]) + + if encodedString is not empty + encodedString.append(delimiter) + + encodedString.append(new_string) + + return encodedString + +func decode(encodedString) + delimiter="," + decodedList = [] + string = "" + + loop index on encodedString + + if encodedString[index] is "\" + string.append(encodedString[index+1]) + index++ + + else if encodedString[index] is delimiter + decodedList.append(string) + string = "" + + else + string.append(encodedString[index]) + + decodedList.append(string) + + return decodedList +``` +## Best Case Scenario +The best-case input for the brute force solution would be a string full of delimiters since the decoding process will finish earlier (the `index` is incremented by 2 while encountering the escape character `\`). + +The time complexity of encoding and decoding in the best-case scenario would be $O(k \times n)$ where $k$ is the average size of the string and $n$ is the size of the input array. + +## Worst Case Scenario +For the worst-case time complexity of the brute-force solution, the input should not contain the delimiter or escape characters. The time complexity of encoding and decoding would still be $O(k \times n)$. + +## Code for Brute Force Solution +```Go +package main + +import "fmt" + +func encode(inputArray []string)(string){ + delimiter := "," + encodedString := "" + + for index:=0;indexOptimized Solution for encoding and decoding strings

+ +To improve on this we can use a special character with the length of the string as delimiters, for example, `["Hello", "World"]` could be encoded to `5;Hello5;World`. + +## Psuedo code for the Optimized Solution +```text +func encode(array) + delimiter = "," + encodedString = "" + loop each element in array + encodedString.append(len(element), delimiter, element) + + return encodedString + +func decode(encodedString) + delimiter = "," + decodedList = [] + + index = 0 + while index + +Thank you for taking the time to read this blog post! If you found this content valuable and would like to stay updated with my latest posts consider subscribing to my RSS Feed. + +# Resources +659 ยท Encode and Decode Strings +Encode and Decode Strings - Leetcode 271 - Python diff --git a/static/avnish-cover.png b/static/avnish-cover.png new file mode 100644 index 0000000..d185d73 Binary files /dev/null and b/static/avnish-cover.png differ