➤ Problem Link : PHONELST
👉 Hint : edit please
✅ C++ Solution :
#include<bits/stdc++.h>
using namespace std;
struct TrieNode
{
struct TrieNode *children[10];
bool isEndOfWord;
};
typedef struct TrieNode TNODE;
bool flag;
TNODE *CreateNewNode()
{
TNODE * node=(TNODE *)malloc(sizeof(TNODE));
for(int i=0;i<10;i++)
node->children[i]=NULL;
node->isEndOfWord=0;
return node;
}
void insert(TNODE * root,string s)
{
TNODE *current=root;
for(int i=0;i<s.length();i++)
{
if(i==s.length()-1 && current->children[s[i]-'0'] != NULL)
{
flag=1;
return;
}
if(current->isEndOfWord==1)
{
flag=1;
return;
}
if(current->children[s[i]-'0']==NULL)
{
TNODE *newNode=CreateNewNode();
current->children[s[i]-'0']=newNode;
}
current=current->children[s[i]-'0'];
}
current->isEndOfWord=1; //True
}
int main()
{
int n,t;
cin>>t;
string s;
while(t--)
{
flag=0;
TNODE *root=CreateNewNode();
root->isEndOfWord=0;
cin>>n;
while(n--)
{
cin>>s;
insert(root,s);
}
if(flag)
cout<<"NO\n";
else
cout<<"YES\n";
}
}
Thank you for your patience reading. If you enjoyed this post, I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Whatsapp or Facebook.
😇Happy Learning!!
