From fa95c917cc138b7563bad85f07e8e125e598502b Mon Sep 17 00:00:00 2001 From: jumpmanmv Date: Sat, 12 Dec 2020 18:33:22 +0200 Subject: [PATCH 1/3] Added solutions to questions 10.1 and 10.2 --- .../10-1-Sorted-Merge/10-1-Sorted-Merge.cpp | 70 +++++++++++++++++++ .../10-2-Group-Anagrams.cpp | 62 ++++++++++++++++ 2 files changed, 132 insertions(+) create mode 100644 Chapter-10-Sorting-and-Searching/10-1-Sorted-Merge/10-1-Sorted-Merge.cpp create mode 100644 Chapter-10-Sorting-and-Searching/10-2-Group-Anagrams/10-2-Group-Anagrams.cpp diff --git a/Chapter-10-Sorting-and-Searching/10-1-Sorted-Merge/10-1-Sorted-Merge.cpp b/Chapter-10-Sorting-and-Searching/10-1-Sorted-Merge/10-1-Sorted-Merge.cpp new file mode 100644 index 0000000..4fe5907 --- /dev/null +++ b/Chapter-10-Sorting-and-Searching/10-1-Sorted-Merge/10-1-Sorted-Merge.cpp @@ -0,0 +1,70 @@ +// Created on: 02/12/2020 (dd/mm/yyyy) +// +// Question 10.1 +// Sorted Merge +// +// Explanation: +// To merge the two arrays, we keep an index of the current element in arrays A and B, and compare the values +// each time to determine which element will be chosen. To avoid creating a third array that holds each value +// temporarily, we start from the end where A has empty space, so that we can use A without overwriting any +// elements. +#include + +void merge(int A[], int N, int B[], int M) +{ + int i = N-1; // current index for array A + int j = M-1; // current index for array B + int k = N+M-1; // current index for merged array + + while (i >= 0 && j >= 0) { + if (i < 0) { + // if we are done with A, just copy the rest of B to the merged array + // if we are done with B, then no need to do anything + while (j >= 0) { + A[k] = B[j]; + j--; + k--; + } + break; + } + if (A[i] <= B[j]) { + A[k] = B[j]; + j--; + } + else { + A[k] = A[i]; + i--; + } + k--; + } + + return; +} + +int main() +{ + // As an example, we can use: + int A[5] = {1, 3, 5, 7, 9}; + int B[4] = {2, 4, 6, 8}; + + // print arrays before + std::cout << "\nArray A before: "; + for (int a = 0; a < 5; a++) { + std::cout << A[a] << " "; + } + std::cout << "\nArray B before: "; + for (int a = 0; a < 4; a++) { + std::cout << B[a] << " "; + } + + merge(A, 5, B, 4); + + // print merged array + int total_size = 4+5; + std::cout << "\nMerged Array: "; + for (int a = 0; a < total_size; a++) { + std::cout << A[a] << " "; + } + + return 0; +} \ No newline at end of file diff --git a/Chapter-10-Sorting-and-Searching/10-2-Group-Anagrams/10-2-Group-Anagrams.cpp b/Chapter-10-Sorting-and-Searching/10-2-Group-Anagrams/10-2-Group-Anagrams.cpp new file mode 100644 index 0000000..596a0f4 --- /dev/null +++ b/Chapter-10-Sorting-and-Searching/10-2-Group-Anagrams/10-2-Group-Anagrams.cpp @@ -0,0 +1,62 @@ +// Created on: 11/12/2020 (dd/mm/yyyy) +// +// Question 10.2 +// Group Anagrams +// +// Explanation: +// To check if two strings are anagrams of each other, we use a simple function that sorts them (by character) and +// checks if they are the same string. To sort the string array we start from the leftmost element and compare it to +// all other elements, moving all the anagrams to the right of it. Then we go to the first element that isn't an +// anagram of these and do the same process until we reach the end of the array. +#include +#include +#include + +bool anagram_check(std::string s1,std::string s2) // checks if two strings are anagrams of each other +{ + if (s1.size() != s2.size()) return false; + std::sort(s1.begin(), s1.end()); + std::sort(s2.begin(), s2.end()); + return s1 == s2; +} + +void group_anagrams(std::string arr[], int size) +{ + int cur_index = 0; + while (cur_index < size) { + // in each loop we start with the element at cur_index and move all the anagrams to the right of it + for (int i = cur_index + 1; i < size; i++) { + if (anagram_check(arr[cur_index], arr[i])) { + cur_index++; + std::string temp = arr[i]; + arr[i] = arr[cur_index]; + arr[cur_index] = temp; + } + } + cur_index++; + } + +} + + +int main() +{ + // As an example, we can use: + std::string s[6] = {"abcd", "asleep", "notananagram", "cdba", "please", "dcba"}; + + // print array before + std::cout << "Array before: "; + for (int i = 0; i < 6; i++) { + std::cout << s[i] << " "; + } + + group_anagrams(s, 6); + + // print array after + std::cout << "\nArray after: "; + for (int i = 0; i < 6; i++) { + std::cout << s[i] << " "; + } + + return 0; +} \ No newline at end of file From 5905cb71482174c0f36210d4da085b00dc27d608 Mon Sep 17 00:00:00 2001 From: jumpmanmv Date: Tue, 27 Apr 2021 23:00:51 +0300 Subject: [PATCH 2/3] Fixed the size of array A in the example to fit all elements --- .../10-1-Sorted-Merge/10-1-Sorted-Merge.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Chapter-10-Sorting-and-Searching/10-1-Sorted-Merge/10-1-Sorted-Merge.cpp b/Chapter-10-Sorting-and-Searching/10-1-Sorted-Merge/10-1-Sorted-Merge.cpp index 4fe5907..661138a 100644 --- a/Chapter-10-Sorting-and-Searching/10-1-Sorted-Merge/10-1-Sorted-Merge.cpp +++ b/Chapter-10-Sorting-and-Searching/10-1-Sorted-Merge/10-1-Sorted-Merge.cpp @@ -44,7 +44,7 @@ void merge(int A[], int N, int B[], int M) int main() { // As an example, we can use: - int A[5] = {1, 3, 5, 7, 9}; + int A[9] = {1, 3, 5, 7, 9}; int B[4] = {2, 4, 6, 8}; // print arrays before From 3259887d385e6f88d58855fbef96006c0c2f0533 Mon Sep 17 00:00:00 2001 From: jumpmanmv Date: Tue, 27 Apr 2021 23:04:43 +0300 Subject: [PATCH 3/3] Changed group_anagram to use std::swap() --- .../10-2-Group-Anagrams/10-2-Group-Anagrams.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Chapter-10-Sorting-and-Searching/10-2-Group-Anagrams/10-2-Group-Anagrams.cpp b/Chapter-10-Sorting-and-Searching/10-2-Group-Anagrams/10-2-Group-Anagrams.cpp index 596a0f4..cfa463a 100644 --- a/Chapter-10-Sorting-and-Searching/10-2-Group-Anagrams/10-2-Group-Anagrams.cpp +++ b/Chapter-10-Sorting-and-Searching/10-2-Group-Anagrams/10-2-Group-Anagrams.cpp @@ -28,9 +28,7 @@ void group_anagrams(std::string arr[], int size) for (int i = cur_index + 1; i < size; i++) { if (anagram_check(arr[cur_index], arr[i])) { cur_index++; - std::string temp = arr[i]; - arr[i] = arr[cur_index]; - arr[cur_index] = temp; + std::swap(arr[i], arr[cur_index]); } } cur_index++;