From 7aa0b7be6f50340cc2ef17ac36d8f63bfe6da4db Mon Sep 17 00:00:00 2001 From: Yashkumar Gupta <50690622+yashh1234@users.noreply.github.com> Date: Wed, 30 Nov 2022 18:12:45 +0530 Subject: [PATCH 1/7] chore(CPlusPlus): add rat in a maze problem (#1051) Co-authored-by: Arsenic <54987647+Arsenic-ATG@users.noreply.github.com> --- .../Backtracking/rat-in-a-maze-problem.cpp | 60 +++++++++++++++++++ algorithms/CPlusPlus/README.md | 1 + 2 files changed, 61 insertions(+) create mode 100644 algorithms/CPlusPlus/Backtracking/rat-in-a-maze-problem.cpp diff --git a/algorithms/CPlusPlus/Backtracking/rat-in-a-maze-problem.cpp b/algorithms/CPlusPlus/Backtracking/rat-in-a-maze-problem.cpp new file mode 100644 index 00000000..0be8f73e --- /dev/null +++ b/algorithms/CPlusPlus/Backtracking/rat-in-a-maze-problem.cpp @@ -0,0 +1,60 @@ +#include +using namespace std; + +bool issafe(int** arr, int x, int y, int n){ + if(x>n; + int** arr=new int*[n]; + for(int i=0; i>arr[i][j]; + } + } + int** solArr=new int*[n]; + for(int i=0; i Date: Wed, 30 Nov 2022 18:15:11 +0530 Subject: [PATCH 2/7] chore(CPlusPlus): add find words matching pattern dictionary (#1050) * Create find_all_words_matching_pattern_in_given_dictionary.cpp Given a dictionary of words where each word follows a CamelCase notation, find all words in it that matches a given pattern of all uppercase characters. We can use a Trie data structure to solve this problem. The idea is to insert all uppercase characters of each word in the CamelCase dictionary into a Trie. Expected output: HiTech HiTechLab HiTechCity * Update README.md --- algorithms/CPlusPlus/README.md | 1 + ...s_matching_pattern_in_given_dictionary.cpp | 120 ++++++++++++++++++ 2 files changed, 121 insertions(+) create mode 100644 algorithms/CPlusPlus/Trie/find_all_words_matching_pattern_in_given_dictionary.cpp diff --git a/algorithms/CPlusPlus/README.md b/algorithms/CPlusPlus/README.md index 5fc38d40..77543aa8 100644 --- a/algorithms/CPlusPlus/README.md +++ b/algorithms/CPlusPlus/README.md @@ -160,6 +160,7 @@ - [Trie for searching](Trie/trie_search.cpp) - [Trie for insert search and prefix_search](Trie/trie_insert_search_startWith.cpp) - [Trie for delete](Trie/trie_delete.cpp) +- [Find Words Matching Pattern Dictionary](Trie/find_all_words_matching_pattern_in_given_dictionary.cpp) # Maths diff --git a/algorithms/CPlusPlus/Trie/find_all_words_matching_pattern_in_given_dictionary.cpp b/algorithms/CPlusPlus/Trie/find_all_words_matching_pattern_in_given_dictionary.cpp new file mode 100644 index 00000000..a1ed6387 --- /dev/null +++ b/algorithms/CPlusPlus/Trie/find_all_words_matching_pattern_in_given_dictionary.cpp @@ -0,0 +1,120 @@ +#include +#include +#include +#include +#include +using namespace std; + +// Data structure to store a Trie node +struct TrieNode +{ + // each node stores a map to its child nodes + unordered_map map; + + // true when the node is a leaf node + bool isLeaf = false; + + // collection to store a complete list of words in the leaf node + unordered_set word; +}; + +// Function to insert a string into a Trie +void insert(TrieNode*& head, string word) +{ + if (head == nullptr) { + head = new TrieNode(); + } + + // start from the head node + TrieNode* curr = head; + for (char c: word) + { + // insert only uppercase characters + if (isupper(c)) + { + // create a new node if the path doesn't exist + if (curr->map.find(c) == curr->map.end()) { + curr->map[c] = new TrieNode(); + } + + // go to the next node + curr = curr->map[c]; + } + } + + // mark the current node as a leaf + curr->isLeaf = true; + + // push the current word into the set associated with a leaf node + (curr->word).insert(word); +} + +// Function to print all children of a given Trie node +void printAllWords(TrieNode* root) +{ + // if the current node is a leaf, print all words associated with it + if (root->isLeaf) + { + unordered_set collection = root->word; + for (string s: collection) { + cout << s << endl; + } + } + + // recur for all children of the root node + for (auto pair: root->map) + { + TrieNode* child = pair.second; + if (child) { + printAllWords(child); + } + } +} + +// Function to print all words in the CamelCase dictionary, which +// matches the given pattern +void findAllWords(vector const &dictionary, string pattern) +{ + // base case + if (dictionary.size() == 0) { + return; + } + + // Trie head node + TrieNode* head = nullptr; + + // construct a Trie from the given dictionary + for (string s: dictionary) { + insert(head, s); + } + + // search for the given pattern in the Trie + TrieNode* curr = head; + for (char c: pattern) + { + // move to the child node + curr = curr->map[c]; + + // if the given pattern is not found (reached end of a path in the Trie) + if (curr == nullptr) { + return; + } + } + + // print all words matching the given pattern + printAllWords(curr); +} + +int main() +{ + vector dictionary { + "Hi", "HiTech", "HiTechCity", "Techie", "TechieDelight", + "Hello", "HelloWorld", "HiTechLab" + }; + + string pattern = "HT"; + + findAllWords(dictionary, pattern); + + return 0; +} From e60d299077fadd58609032eeaedfb59dc89b4e79 Mon Sep 17 00:00:00 2001 From: Aditya Sharma Date: Wed, 30 Nov 2022 07:56:18 -0500 Subject: [PATCH 3/7] docs: fix grammar and punctuation errors (#1087) Co-authored-by: Aditya Sharma --- CONTRIBUTING.md | 4 ++-- README.md | 2 +- docs/en/Searching/Binary-Search.MD | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index c714868c..b8565a3e 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -2,7 +2,7 @@ This documentation aims to simplify and guide the way beginners make their first contribution. If you are looking to make your first contribution, follow the steps below. -_If you're not comfortable with command line, [here are tutorials using GUI tools.](#tutorials-using-other-tools)_ +_If you're not comfortable with the command line, [here are tutorials using GUI tools.](#tutorials-using-other-tools)_ fork this repository @@ -61,7 +61,7 @@ git checkout -b add-new-file ## Make necessary changes and commit those changes -Now open add or edit file in a text editor. Add code for any existing algorithm in other language or add some new algorithms. Make sure to update correspond README.md file if needed. Now, save the file. +Now open add or edit file in a text editor. Add code for any existing algorithm in other language or add some new algorithms. Make sure to update the corresponding README.md file if needed. Now, save the file. git status diff --git a/README.md b/README.md index c8d8263b..62ef05c0 100644 --- a/README.md +++ b/README.md @@ -105,7 +105,7 @@ It can be any of the following ones #### Source Code File -The source code files, should either be in `src/` folder (**Eg.** `src/main.cpp` or `src/main.js`) or the root folder (**Eg.** `palindrome.go` or `App.java`) where `ext` is the file extension for the specific programming language. +The source code files should either be in `src/` folder (**Eg.** `src/main.cpp` or `src/main.js`) or the root folder (**Eg.** `palindrome.go` or `App.java`) where `ext` is the file extension for the specific programming language. Again, the source codes must conform to a valid file structure convention that the programming language enforces. diff --git a/docs/en/Searching/Binary-Search.MD b/docs/en/Searching/Binary-Search.MD index dbaadaac..6e4da5e1 100644 --- a/docs/en/Searching/Binary-Search.MD +++ b/docs/en/Searching/Binary-Search.MD @@ -13,7 +13,7 @@ 1. Find the middle element of the array 2. Check whether the key is equal to middle element if yes then return the index and exit the program 3. If the 2 step didn't run then test whether the element is less than the middle element if yes then run the step: 1 between the start to middle-1 index -4. If the 3 step didn't run then test whether the element is high than the middle element if yes then run the step: 1 between the middle+1 to last index. +4. If the 3 step didn't run then test whether the element is higher than the middle element if yes then run the step: 1 between the middle+1 to the last index. 5. Run the loop till the starting index is less than end index 6. If the loop over and data not found then return -1 that means data doesn't exist > **Note:** The array should be sorted in ascending to descending order @@ -26,7 +26,7 @@ Element to search: **20** Procedure: -Middle element:**30** and element is less then 30 so search between start to middle -1 index +Middle element:**30** and element is less than 30 so search between start to middle -1 index Middle element: **20** and yes the middle element is the key to found so return the index=**1** From 6620f32d9c79477ddb3e38ee390a81e16c35a79a Mon Sep 17 00:00:00 2001 From: RK-Shandilya <89962501+RK-Shandilya@users.noreply.github.com> Date: Wed, 14 Dec 2022 23:56:00 +0530 Subject: [PATCH 4/7] chore(CPlusPlus): add reverse the string wordwise (#1100) --- algorithms/CPlusPlus/README.md | 1 + .../Strings/ReverseTheStringWordwise.cpp | 48 +++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 algorithms/CPlusPlus/Strings/ReverseTheStringWordwise.cpp diff --git a/algorithms/CPlusPlus/README.md b/algorithms/CPlusPlus/README.md index 77543aa8..6bd7c105 100644 --- a/algorithms/CPlusPlus/README.md +++ b/algorithms/CPlusPlus/README.md @@ -136,6 +136,7 @@ - [Longest common prefix](Strings/longest-common-prefix.cpp) - [First unique character in the string](Strings/first-unique-character.cpp) - [Sliding Window to match target string](Strings/sliding-window.cpp) +- [Reverse String Wordwise](Strings/ReverseTheStringWordwise.cpp) ## Trees diff --git a/algorithms/CPlusPlus/Strings/ReverseTheStringWordwise.cpp b/algorithms/CPlusPlus/Strings/ReverseTheStringWordwise.cpp new file mode 100644 index 00000000..8c221840 --- /dev/null +++ b/algorithms/CPlusPlus/Strings/ReverseTheStringWordwise.cpp @@ -0,0 +1,48 @@ +// Description :- Given a string, the task is to reverse the order of the words in the given string. +// Example :- +// Input 1: +// A = "the sky is blue" +// Input 2: +// A = "this is ib" +// Output 1: +// "blue is sky the" +// Output 2: +// "ib is this" + + +// Time Complexity = O(N), Space Complexity = O(N) + +#include +using namespace std; + +string solve(string s) { + vectorv; + string str=""; + for(int i=0;i0;i--){ + str+=v[i]; + str+=' '; + } + str+=v[0]; + return str; +} + +int main() +{ + string s; + getline(cin, s); + cout< Date: Thu, 15 Dec 2022 00:10:30 +0530 Subject: [PATCH 5/7] docs(en): enhancement the linear search (#1096) I noticed the steps in the algorithm to be continuous which would make it inconvenient for the reader. So, I reformatted the steps to look much cleaner and readable. --- docs/en/Searching/Linear-Search.md | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/docs/en/Searching/Linear-Search.md b/docs/en/Searching/Linear-Search.md index 1d4487ad..26975c3c 100644 --- a/docs/en/Searching/Linear-Search.md +++ b/docs/en/Searching/Linear-Search.md @@ -10,13 +10,20 @@ Linear search is usually very **simple to implement**. ## Steps/Algorithm: **Linear Search( Array A, Value x)** -Step 1: Set i to 1 -Step 2: if i > n then go to step 7 -Step 3: if A[i] = x then go to step 6 -Step 4: Set i to i + 1 -Step 5: Go to Step 2 -Step 6: Print Element x Found at index i and go to step 8 -Step 7: Print element not found +Step 1: Set i to 1 + +Step 2: if i > n then go to step 7 + +Step 3: if A[i] = x then go to step 6 + +Step 4: Set i to i + 1 + +Step 5: Go to Step 2 + +Step 6: Print Element x Found at index i and go to step 8 + +Step 7: Print element not found + Step 8: Exit ## Pseudocode From bb641ee600cec57d727ccaf9373654349c9d318c Mon Sep 17 00:00:00 2001 From: RK-Shandilya <89962501+RK-Shandilya@users.noreply.github.com> Date: Thu, 22 Dec 2022 18:58:01 +0530 Subject: [PATCH 6/7] chore(CPlusPlus): add reverse in groups of K (#1106) --- .../Linked-Lists/reverseInGroupsOfK.cpp | 79 +++++++++++++++++++ algorithms/CPlusPlus/README.md | 1 + 2 files changed, 80 insertions(+) create mode 100644 algorithms/CPlusPlus/Linked-Lists/reverseInGroupsOfK.cpp diff --git a/algorithms/CPlusPlus/Linked-Lists/reverseInGroupsOfK.cpp b/algorithms/CPlusPlus/Linked-Lists/reverseInGroupsOfK.cpp new file mode 100644 index 00000000..a0de9bb8 --- /dev/null +++ b/algorithms/CPlusPlus/Linked-Lists/reverseInGroupsOfK.cpp @@ -0,0 +1,79 @@ +// Description := Reverse a linkedlist in groups of size K . + +// Time and space complexity :- +// Time Complexity = O(N) and Space Complexity = O(N) + +// Example :- +// Input: 1->2->3->4->5->6->7->8->NULL, K = 3 +// Output: 3->2->1->6->5->4->8->7->NULL + +#include +using namespace std; + +class Node{ + public: + int data; + Node* next; +}; + +void push(Node* &head_ref, int new_data) +{ + Node* new_node = new Node(); + + new_node->data = new_data; + + new_node->next = head_ref; + + head_ref = new_node; +} + +void printList(Node* node) +{ + while (node != NULL) { + cout << node->data << " "; + node = node->next; + } +} + +Node* kReverse(Node* &head, int k) { + + // base case + if(head == NULL) { + return NULL; + } + + Node* next = NULL; + Node* curr = head; + Node* prev = NULL; + int count= 0; + + while( curr != NULL && count < k ) { + next = curr -> next; + curr -> next = prev; + prev = curr; + curr = next; + count++; + } + + if(next != NULL) { + head -> next = kReverse(next,k); + } + return prev; +} + +int main(){ + Node* head=NULL; + Node* ans =head; + push(head,1); + push(head,2); + push(head,3); + push(head,4); + push(head,5); + push(head,6); + push(head,7); + push(head,8); + printList(head); + head = kReverse(head,3); + cout< Date: Thu, 22 Dec 2022 18:59:25 +0530 Subject: [PATCH 7/7] chore(CPlusPlus): add largest and smallest number in an array (#1110) --- .../CPlusPlus/Arrays/Largest-smallest.cpp | 20 +++++++++++++++++++ algorithms/CPlusPlus/README.md | 2 +- 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 algorithms/CPlusPlus/Arrays/Largest-smallest.cpp diff --git a/algorithms/CPlusPlus/Arrays/Largest-smallest.cpp b/algorithms/CPlusPlus/Arrays/Largest-smallest.cpp new file mode 100644 index 00000000..17b9829f --- /dev/null +++ b/algorithms/CPlusPlus/Arrays/Largest-smallest.cpp @@ -0,0 +1,20 @@ +#include +#include +using namespace std; + +//simple approach: +//sort the array in ascending order. +//the first element would be the smallest and the last element would be the largest + +int main() +{ + int arr[]={1,2,3,4,5}; + int n=sizeof(arr)/sizeof(arr[0]); + cout<a is the name of the array and n is the size of array a + cout< smallest number "< largest number "<