Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kth_smallest_element_in_an_unsorted_array #301

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions Easy/word_pattern.cpp
Original file line number Diff line number Diff line change
@@ -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<bits/stdc++.h>
using namespace std;
class Solution {
public:
bool wordPattern(string pattern, string s) {
unordered_map<char,string>umap1;
unordered_map<string,char>umap2;
vector<string>answer;
string st=" ";
for(int i=0;i<s.size();i++){
if(s[i]==' '){
answer.push_back(st);
st=" ";
}
else{
st.push_back(s[i]);
}
}
answer.push_back(st);
if(answer.size()!=pattern.length())
return false;

for(int i=0;i<answer.size();i++){
if(umap2.find(answer[i])!=umap2.end()){
if(umap2[answer[i]]!=pattern[i])
return false;
}
if(umap1.find(pattern[i])!=umap1.end()){
if(umap1[pattern[i]]!=answer[i])
return false;
}
umap1[pattern[i]]=answer[i];
umap2[answer[i]]=pattern[i];
}
return true;
}
};

24 changes: 24 additions & 0 deletions Medium/Kth_smallest_element_in_array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// problem statement
// find the kth smallest element in an unsorted array

#include <bits/stdc++.h>
using namespace std;

int main()
{
int arr[] = { 12, 3, 5, 7, 19 };
int N = sizeof(arr) / sizeof(arr[0]);
int K = 4;

set<int> s(arr, arr + N);

// s.begin() returns a pointer to first
// element in the set
set<int>::iterator itr = s.begin();

advance(itr, K - 1); // itr points to kth element in set

cout << *itr << "\n";

return 0;
}