From baf54ba49ee908a8d7e806ab9491699d9cfc2d81 Mon Sep 17 00:00:00 2001 From: Utsav Mani Tiwari Date: Thu, 27 Oct 2022 01:26:37 +0530 Subject: [PATCH 1/2] Kth_smallest_element_in_an_unsorted_array --- Medium/Kth_smallest_element_in_array.cpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Medium/Kth_smallest_element_in_array.cpp diff --git a/Medium/Kth_smallest_element_in_array.cpp b/Medium/Kth_smallest_element_in_array.cpp new file mode 100644 index 0000000..534ef1a --- /dev/null +++ b/Medium/Kth_smallest_element_in_array.cpp @@ -0,0 +1,24 @@ +// problem statement +// find the kth smallest element in an unsorted array + +#include +using namespace std; + +int main() +{ + int arr[] = { 12, 3, 5, 7, 19 }; + int N = sizeof(arr) / sizeof(arr[0]); + int K = 4; + + set s(arr, arr + N); + + // s.begin() returns a pointer to first + // element in the set + set::iterator itr = s.begin(); + + advance(itr, K - 1); // itr points to kth element in set + + cout << *itr << "\n"; + + return 0; +} From 92f6489ba7043e2134c92903a213987ef1206775 Mon Sep 17 00:00:00 2001 From: Utsav Mani Tiwari Date: Fri, 28 Oct 2022 00:13:01 +0530 Subject: [PATCH 2/2] word_pattern leetcode easy tagged question --- Easy/word_pattern.cpp | 64 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 Easy/word_pattern.cpp diff --git a/Easy/word_pattern.cpp b/Easy/word_pattern.cpp new file mode 100644 index 0000000..d685958 --- /dev/null +++ b/Easy/word_pattern.cpp @@ -0,0 +1,64 @@ +// Given a pattern and a string s, find if s follows the same pattern. + +// Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in s. + + + +// Example 1:pattern = "abba", s = "dog cat cat dog" + // true + + +/* +Logic && intution +It's pretty intutive since as we can clearly understand by reading this question that there should be one to one mapping first thing ,second think of the edge case then you will be able to get the logic. +For implementation part you can go through the above solution. + +First create two unordered map for mapping char to string and string to char +create an answer vector to store +have an empty string st,traverse through it and if you you encounter empty space add it to answer vector +and keep string as it is. Lastly put all the string words in st and then in answer vector. +Here,comes the most important thing to understand +a) while traversing the answer vector check in the map if its found and if found then whether correctly mapped or not ,in case it is not correctly mapped then return false. (string to char) +b) similarly traverse the characters and check if found in the map ,if found but not mapped correctly then return false. +c) Exist the loop and map char to string and string to map**** + +*/ + +#include +using namespace std; +class Solution { +public: + bool wordPattern(string pattern, string s) { + unordered_mapumap1; + unordered_mapumap2; + vectoranswer; + string st=" "; + for(int i=0;i