diff --git a/README.md b/README.md index 7189779..c9bb8cf 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,8 @@ Blogs on Self-Hosting, Homelab and DevOps Technologies. - Rabin-Karp Substring Search - Checking an Array for Duplicate Values - Identifying Anagrams - - Finding Elements in an Array that Sum Up to a Target Value + - Finding Elements in an Array that Sum Up to a Target Value + - Group Anagrams in an Array ## Technologies Used - [Hugo Static Site Generator](https://gohugo.io/) diff --git a/config.yml b/config.yml index b1265ac..7f7ce03 100644 --- a/config.yml +++ b/config.yml @@ -30,9 +30,9 @@ languages: - name: Archive url: archives weight: 5 - # - name: Index - # url: https://github.com/bovem/bovem.github.io#index - # weight: 10 + - name: Index + url: contents + weight: 10 - name: Tags url: tags/ weight: 15 diff --git a/content/contents.md b/content/contents.md new file mode 100644 index 0000000..0c13808 --- /dev/null +++ b/content/contents.md @@ -0,0 +1,36 @@ +--- +title: "Index" +layout: "contents" +url: "/contents" +summary: "Index of all content" +--- + +- 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 + diff --git a/content/posts/dsa/contains-duplicate/index.md b/content/posts/dsa/contains-duplicate/index.md index 8159bf1..83497ac 100644 --- a/content/posts/dsa/contains-duplicate/index.md +++ b/content/posts/dsa/contains-duplicate/index.md @@ -3,7 +3,7 @@ author: "Avnish" title: "Checking an Array for Duplicate Values" date: "2023-10-10" description: "Implementing a containsDuplicate function that returns true if there are duplicate elements present in the array and false otherwise" -tags: ["data-structures", "arrays", "hashmaps", "go", "neetcode-150"] +tags: ["data-structures", "arrays", "hashmaps", "go", "neetcode-150", "leetcode-easy"] categories: ["Data Structures"] series: ["Data Structures and Algorithms"] aliases: ["contains-duplicate"] diff --git a/content/posts/dsa/finding-elements-that-sum-up-to-target/index.md b/content/posts/dsa/finding-elements-that-sum-up-to-target/index.md index 14a2bde..d219d35 100644 --- a/content/posts/dsa/finding-elements-that-sum-up-to-target/index.md +++ b/content/posts/dsa/finding-elements-that-sum-up-to-target/index.md @@ -3,7 +3,7 @@ author: "Avnish" title: "Finding Elements in an Array that Sum Up to a Target Value" date: "2023-10-15" description: "Implementing a twoSums(inputArray, targetValue) function that returns the indices of two elements in inputArray which could be summed up to the targetValue" -tags: ["data-structures", "arrays", "hashmaps", "go", "neetcode-150"] +tags: ["data-structures", "arrays", "hashmaps", "go", "neetcode-150", "leetcode-easy"] categories: ["Data Structures"] series: ["Data Structures and Algorithms"] aliases: ["finding-elements-that-sum-up-to-target", "two-sums"] diff --git a/content/posts/dsa/group-anagrams-in-an-array/group-anagrams-brute-force-best.png b/content/posts/dsa/group-anagrams-in-an-array/group-anagrams-brute-force-best.png new file mode 100644 index 0000000..6b04367 Binary files /dev/null and b/content/posts/dsa/group-anagrams-in-an-array/group-anagrams-brute-force-best.png differ diff --git a/content/posts/dsa/group-anagrams-in-an-array/group-anagrams-brute-force-worst.png b/content/posts/dsa/group-anagrams-in-an-array/group-anagrams-brute-force-worst.png new file mode 100644 index 0000000..2da1132 Binary files /dev/null and b/content/posts/dsa/group-anagrams-in-an-array/group-anagrams-brute-force-worst.png differ diff --git a/content/posts/dsa/group-anagrams-in-an-array/group-anagrams-brute-force.png b/content/posts/dsa/group-anagrams-in-an-array/group-anagrams-brute-force.png new file mode 100644 index 0000000..69aae6b Binary files /dev/null and b/content/posts/dsa/group-anagrams-in-an-array/group-anagrams-brute-force.png differ diff --git a/content/posts/dsa/group-anagrams-in-an-array/group-anagrams-cover.png b/content/posts/dsa/group-anagrams-in-an-array/group-anagrams-cover.png new file mode 100644 index 0000000..3b3bd6d Binary files /dev/null and b/content/posts/dsa/group-anagrams-in-an-array/group-anagrams-cover.png differ diff --git a/content/posts/dsa/group-anagrams-in-an-array/group-anagrams-optimized.png b/content/posts/dsa/group-anagrams-in-an-array/group-anagrams-optimized.png new file mode 100644 index 0000000..93d19db Binary files /dev/null and b/content/posts/dsa/group-anagrams-in-an-array/group-anagrams-optimized.png differ diff --git a/content/posts/dsa/group-anagrams-in-an-array/group-anagrams-problem.png b/content/posts/dsa/group-anagrams-in-an-array/group-anagrams-problem.png new file mode 100644 index 0000000..43d684f Binary files /dev/null and b/content/posts/dsa/group-anagrams-in-an-array/group-anagrams-problem.png differ diff --git a/content/posts/dsa/group-anagrams-in-an-array/index.md b/content/posts/dsa/group-anagrams-in-an-array/index.md new file mode 100644 index 0000000..2c9e301 --- /dev/null +++ b/content/posts/dsa/group-anagrams-in-an-array/index.md @@ -0,0 +1,340 @@ +--- +author: "Avnish" +title: "Group Anagrams in an Array" +date: "2023-10-22" +description: "Implementing a groupAnagrams function that takes an array of strings as input and returns another array with anagrams grouped together" +tags: ["data-structures", "strings", "hashmaps", "go", "neetcode-150", "leetcode-medium"] +categories: ["Data Structures"] +series: ["Data Structures and Algorithms"] +aliases: ["group-anagrams-in-an-array", "group-anagrams"] +ShowToc: true +TocOpen: false +comments: false +cover: + image: "group-anagrams-cover.png" + linkFullImages: true + alt: "The groupAnagrams function will return an array with anagrams grouped together" + caption: "" + relative: false + responsiveImages: false +math: true +--- + +# Problem Statement +We have to implement a `groupAnagram` function that takes an array of strings as input and returns a new array with anagrams grouped. + +
+ +The input string array is assumed to be composed entirely of lowercase English characters. + +# Brute Force Solution +If two strings are anagrams then their sorted order will be the same. Thus, anagrams could be grouped under their sorted order. + + + +The brute-force implementation of `groupAnagram` uses a hashmap for grouping. In this hashmap the sorted string will be the key and the original strings will be the values (stored in an array). At the end of the function, we can iterate over the hashmap and create an array of grouped anagrams. + +## Psuedo-code for the Brute Force Solution +```text +hashmap = HashMap() +loop index in stringArray + stringValue = stringArray[index] + sortedString = string(sort(stringValue)) + if hashmap[sortedString] + hashmap[sortedString].append(stringValue) + else + hashmap[sortedString] = [stringValue] + +returnedList = [] +loop key, value in hashmap + returnedList.append(value) + +return returnedList +``` + +## Best Case Scenario +The time taken to sort individual strings is assumed to be $O(k \log(k))$ (the time complexity of the best sorting algorithm) where $k$ is the size of the string. Since we have to perform this operation on every element the sorting operation will be repeated $n$ time, where $n$ is the size of the array, resulting in $O(n \times k \log(k))$ time. + + + +The best-case input will contain just a single set of anagrams i.e. the hashmap will contain just one key. The loop over hashmap values will be executed only once. Thus, the total time complexity of grouping anagrams will be $O(n \times k \log(k))$. + +## Worst Case Scenario + + +The worst-case input array will contain zero anagrams and the hashmap will contain a key for each array element i.e. $n$ keys. Thus, the total time complexity of the function in the worst-case scenario will be $O(n + n \times k \log(k))$, which could be simplified to $O(n \times k \log(k))$. + +## Code for Brute Force Solution +```Go +package main + +import ( + "fmt" + "sort" + "strings" +) + +func sortString(inputString string)(string){ + listInputString := strings.Split(inputString, "") + sort.Strings(listInputString) + return strings.Join(listInputString, "") +} + +func groupAnagrams(inputArray []string)([][]string){ + hashmap := make(map[string][]string) + + // Loop over the inputArray and sort every string element + for index:=0;index