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

Prefix Strings (Trie): Removing lines of code that resulted duplicates #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
114 changes: 60 additions & 54 deletions DSA Essentials Solutions/Trie/PrefixStrings.cpp
Original file line number Diff line number Diff line change
@@ -1,80 +1,86 @@
#include <bits/stdc++.h>
using namespace std;

class node{
public:
class node
{
public:
char ch;
unordered_map<char,node*> next;
unordered_map<char, node *> next;
bool isTerminal;
node(char a){
ch=a;
bool isTerminal=false;
node(char a)
{
ch = a;
bool isTerminal = false;
}
};
class Trie{
public:
node*root= new node('\0');

void insert(string str){
class Trie
{
public:
node *root = new node('\0');

node*temp=root;
for(int i=0; i<str.length(); i++){
if(temp->next.count(str[i])==0){
temp->next[str[i]]=new node(str[i]);
}
temp=temp->next[str[i]];
void insert(string str)
{

node *temp = root;
for (int i = 0; i < str.length(); i++)
{
if (temp->next.count(str[i]) == 0)
{
temp->next[str[i]] = new node(str[i]);
}
temp = temp->next[str[i]];
}
temp->isTerminal=true;
temp->isTerminal = true;
return;
}
void dfs(node*temp, vector<string> &v, string word ){
if(temp->isTerminal){

void dfs(node *temp, vector<string> &v, string word)
{
if (temp->isTerminal)
{
v.push_back(word);
}
if(temp->next.empty()){
if (temp->next.empty())
{
return;
}
for(auto p:temp->next){
word.push_back(p.first);
dfs(temp->next[p.first],v,word);
word.pop_back();
for (auto p : temp->next)
{
word.push_back(p.first);
dfs(temp->next[p.first], v, word);
word.pop_back();
}
return;
}



vector<string> find(string str){
vector<string> find(string str)
{
vector<string> v;
node* temp=root;
string word="";
for(int i=0; i<str.length(); i++){
if(temp->next.count(str[i])==0){
return v;
}
word.push_back(str[i]);
temp=temp->next[str[i]];
node *temp = root;
string word = "";
for (int i = 0; i < str.length(); i++)
{
if (temp->next.count(str[i]) == 0)
{
return v;
}
word.push_back(str[i]);
temp = temp->next[str[i]];
}
if(temp->isTerminal){
v.push_back(word);
}
dfs(temp,v,word);
sort(v.begin(),v.end());

dfs(temp, v, word);
sort(v.begin(), v.end());
return v;
}




};



vector<string> findPrefixStrings(vector<string> words, string prefix){
Trie t;
for(auto s:words){
t.insert(s);
}
vector<string> res=t.find(prefix);
return res;
vector<string> findPrefixStrings(vector<string> words, string prefix)
{
Trie t;
for (auto s : words)
{
t.insert(s);
}
vector<string> res = t.find(prefix);
return res;
}