Skip to content

Added Optimized problem set on Trie and Dp on stocks #212

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

Closed
wants to merge 1 commit into from
Closed
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
127 changes: 127 additions & 0 deletions Trie/TrieNew.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
#include <stdio.h>
#include <iostream>
#include <vector>
using namespace std;

struct Trie
{
public:
Trie *children[26] = {};
bool isWord = false;

void insert(string &word)
{
Trie *curr = this;
for (char c : word)
{

if (curr->children[c - 'a'] == nullptr)
{

curr->children[c - 'a'] = new Trie();
}
curr = curr->children[c - 'a'];
}
curr->isWord = true;
cout << "insertion took palce successfully!!" << endl;
}
bool search()
{
string word;
cout << "enter the string you want to search" << endl;
cin >> word;
Trie *curr = this;
for (char &c : word)
{
if (!curr->children[c - 'a'])
{

return false;
}

curr = curr->children[c - 'a'];
}
if (curr->isWord)
{
cout << "the word is present" << endl;
}
else
{
cout << "the word is not present:" << endl;
}

return curr->isWord;
}
bool searchWithin()
{
string prefixWord;
cout << "enter the prefixWord you want to check" << endl;
cin >> prefixWord;
Trie *curr = this;
for (char c : prefixWord)
{
if (!curr->children[c - 'a'])
return false;
curr = curr->children[c - 'a'];
}
cout << "the prefix word is present in the trie" << endl;
return true;
}
};

int main()
{
Trie trie;
int n;
cout << "enter the number of strings you want to store in vector:" << endl;
cin >> n;

vector<string> store(n);
for (int i = 0; i < n; i++)
{
string temp;
cout << "enter the string:" << endl;
cin >> temp;
store[i] = temp;
}
for (int i = 0; i < n; i++)
{
cout << store[i] << endl;
}
cout << "choose number 1 to insert all elements to trie" << endl;
cout << "1:insert words" << endl;
int number;
cin >> number;
switch (number)
{
case 1:
cout << "insertion of trie takes place!" << endl;
for (int i = 0; i < n; i++)
{
trie.insert(store[i]);
}
break;

default:
break;
}
cout << endl;

cout << "Choose 2:search \t Choose 3:search Prefix" << endl;
int number1;
cout << "enter the Option:" << endl;
cin >> number1;

switch (number1)
{
case 2:
trie.search();
break;
case 3:
trie.searchWithin();

default:
break;
}
return 0;
}
108 changes: 108 additions & 0 deletions Trie/WordSearch2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#include <stdio.h>
#include <iostream>
#include <vector>
using namespace std;

struct Trie
{
Trie *children[26] = {};
string *Word;

void fillWords(string &word)
{
Trie *curr = this;
for (char c : word)
{
if (curr->children[c - 'a'] == nullptr)
{
curr->children[c - 'a'] = new Trie();
}
curr = curr->children[c - 'a'];
}
curr->Word = &word;
}
};
vector<string> ans;

void dfs(int i, int j, int m, int n, vector<vector<char>> &grid, Trie *curr)
{
if (i < 0 || i >= m || j < 0 || j >= n || grid[i][j] == '.' || curr->children[grid[i][j] - 'a'] == nullptr)
return;
char c = grid[i][j];
curr = curr->children[c - 'a'];
if (curr->Word != nullptr)
{
ans.push_back(*curr->Word);
curr->Word = nullptr;
}
dfs(i + 1, j, m, n, grid, curr);
dfs(i - 1, j, m, n, grid, curr);
dfs(i, j + 1, m, n, grid, curr);
dfs(i, j - 1, m, n, grid, curr);
grid[i][j] = c;
}

vector<string> findWords(vector<vector<char>> &grid, vector<string> &word)
{
int m = grid.size();
int n = grid[0].size();
Trie trie;
for (string &s : word)
{
trie.fillWords(s);
}
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
dfs(i, j, m, n, grid, &trie);
}
}

return ans;
}

int main()
{
int row;
int col;

cout << "enter the row size of the matrix:" << endl;
cin >> row;
cout << "enter the column size of the matrix:" << endl;
cin >> col;

vector<vector<char>> grid(row, vector<char>(col));

for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
char c;
cout << "enter the character to the grid:" << endl;
cin >> c;
grid[i][j] = c;
}
cout << endl;
}
cout << endl;
int n;
cout << "enter the string vector:" << endl;
cin >> n;
vector<string> s(n);
for (int i = 0; i < n; i++)
{
string temp;
cout << "enter the strings to list:" << endl;
cin >> temp;
s[i] = temp;
}
cout << endl;
vector<string> ans = findWords(grid, s);
for (int i = 0; i < ans.size(); i++)
{
cout << ans[i] << endl;
}

return 0;
}
134 changes: 134 additions & 0 deletions Trie/trieDataStructure.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
#include <stdio.h>
#include <iostream>
#include <vector>
using namespace std;

struct Node
{
Node *links[26];
bool flag = false;
string *word;

bool checkKey(char c)
{
return links[c - 'a'];
}
void put(char c, Node *node)
{
links[c - 'a'] = node;
}
Node *get(char ch)
{
return links[ch - 'a'];
}
void setEnd()
{
flag = true;
}
bool isEnd()
{
return flag;
}
};

class Trie
{
private:
Node *root;

public:
Trie()
{
// creating new object
root = new Node();
}

bool insert(string words)
{
Node *node = root;
for (int i = 0; i < words.size(); i++)
{
if (!node->checkKey(words[i]))
{
node->put(words[i], new Node());
}
node = node->get(words[i]);
}
node->setEnd();
node->word = &words;

return true;
}

bool search(string word)
{
Node *node = root;

for (int i = 0; i < word.size(); i++)
{
if (!node->checkKey(word[i]))
{
return false;
}
node = node->get(word[i]);
}
if (node->isEnd())
{

return true;
}
return false;
}
bool startsWithin(string prefixWord)
{
Node *node = root;
for (int i = 0; i < prefixWord.size(); i++)
{
if (!node->checkKey(prefixWord[i]))
{
return false;
}
node = node->get(prefixWord[i]);
}
return true;
}
};

int main()
{
Node *node;
Trie trie;

int n;
cout << "enter the number of words to insert in the string:" << endl;
cin >> n;

vector<string> words(n);
for (int i = 0; i < n; i++)
{
string temp;
cout << "enter the string you want to insert:" << endl;
cin >> temp;
words[i] = temp;
}

cout << "insert the string in to the trie:" << endl;

for (int i = 0; i < n; i++)
{
if (trie.insert(words[i]))
cout << "inserted all words successfully!" << endl;
}
bool searchAnswer = trie.search(words[0]);
if (searchAnswer)
{

cout << "word is present in the trie::" << endl;
}
else
{
cout << "word is not present" << endl;
}

return 0;
}
Loading