Skip to content

Commit ea4117e

Browse files
authored
Merge pull request #4 from naman-narula/master
Create codingninjas.cpp
2 parents d34e8e4 + bb514e5 commit ea4117e

File tree

2 files changed

+172
-0
lines changed

2 files changed

+172
-0
lines changed

Graphs1/codingninjas.cpp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
bool check(char graph[][MAXN],int n,int m,bool visited[][MAXN],string s,int i,int j)
2+
{
3+
if(s[0]=='\0')
4+
return true;
5+
if(visited[i][j]==true)
6+
return false;
7+
visited[i][j]=true;
8+
bool ans=false;
9+
if(graph[i-1][j-1]==s[0]&&(i-1>=0&&j-1>=0))
10+
{
11+
ans=check(graph,n,m,visited,s.substr(1),i-1,j-1);
12+
if(ans)
13+
return true;
14+
}
15+
if(graph[i-1][j]==s[0]&&i-1>=0)
16+
{ ans=check(graph,n,m,visited,s.substr(1),i-1,j);
17+
if(ans)
18+
return true;
19+
}
20+
if(graph[i-1][j+1]==s[0]&&(i-1>=0&&j+1<m))
21+
{ ans = check(graph,n,m,visited,s.substr(1),i-1,j+1);
22+
if(ans)
23+
return true;
24+
}
25+
if(graph[i][j+1]==s[0] && j+1<m)
26+
{ ans = check(graph,n,m,visited,s.substr(1),i,j+1);
27+
if(ans)
28+
return true;
29+
}
30+
if(graph[i+1][j+1]==s[0] && (j+1<m&&i+1<n))
31+
{
32+
ans = check(graph,n,m,visited,s.substr(1),i+1,j+1);
33+
if(ans)
34+
return true;
35+
}
36+
if(graph[i+1][j]==s[0] && i+1<n)
37+
{ans= check(graph,n,m,visited,s.substr(1),i+1,j);
38+
if(ans)
39+
return true;
40+
}
41+
if(graph[i+1][j-1]==s[0] && (j-1>=0&&i+1<n))
42+
{ans = check(graph,n,m,visited,s.substr(1),i+1,j-1);
43+
if(ans)
44+
return true;
45+
}
46+
if(graph[i][j-1]==s[0] && j-1>=0)
47+
{ans = check(graph,n,m,visited,s.substr(1),i,j-1);
48+
if(ans)
49+
return true;
50+
}
51+
visited[i][j]=false;
52+
if(ans==false)
53+
return false;
54+
55+
56+
}
57+
int solve(char graph[][MAXN],int N, int M)
58+
{
59+
// Write your code here.
60+
bool visited[N][MAXN]={false};
61+
string s="CODINGNINJA";
62+
for(int k=0;k<N;k++)
63+
{
64+
for(int l=0;l<M;l++)
65+
{ if(graph[k][l]=='C')
66+
{
67+
if(check(graph,N,M,visited,s.substr(1),k,l))
68+
return true;
69+
}
70+
}
71+

Tries/autocomplete.cpp

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
class trienode{
4+
public:
5+
char letter;
6+
bool eow;//end of word
7+
trienode** children=new trienode* [26];
8+
trienode()
9+
{
10+
letter='\0';
11+
eow=false;
12+
for(int i=0;i<26;i++)
13+
children[i]=NULL;
14+
}
15+
};
16+
void insert(string s,trienode* head)
17+
{
18+
trienode* curr= head;
19+
int i=0;
20+
for(i=0;i<s.length();i++)
21+
{
22+
int index=s[i]-'a';
23+
if(curr->children[index]!=NULL)
24+
{
25+
curr=curr->children[index];
26+
}
27+
else
28+
{
29+
curr->children[index]=new trienode();
30+
curr->children[index]->letter=s[i];
31+
curr=curr->children[index];
32+
}
33+
}
34+
35+
if(i==s.length())
36+
{
37+
curr->eow=true;
38+
}
39+
40+
}
41+
void dfs(trienode* root,string in)
42+
{ if(root->eow)
43+
cout<<in<<endl;
44+
45+
for(int i=0;i<26;i++)
46+
{ if(root->children[i])
47+
dfs(root->children[i],in+root->children[i]->letter);
48+
}
49+
50+
}
51+
int complete(string search,trienode* head)
52+
{ trienode* curr = head;
53+
for(int i=0;i<search.length();i++)
54+
{
55+
int index=search[i]-'a';
56+
if(curr->children[index]!=NULL)
57+
{
58+
curr=curr->children[index];
59+
}
60+
else
61+
{
62+
return 0;
63+
}
64+
}
65+
dfs(curr,search);
66+
return 1;
67+
}
68+
int main()
69+
{int n;
70+
cin>>n;
71+
string input;
72+
trienode* head=new trienode();
73+
for(int i=0;i<n;i++)
74+
{
75+
cin>>input;
76+
insert(input,head);
77+
}
78+
/* for(int i=0;i<26;i++)
79+
{
80+
if(head->children[i])
81+
{
82+
cout<<head->children[i]->letter<<endl;
83+
}
84+
}*/
85+
int q;
86+
cin>>q;
87+
string search;
88+
int result;
89+
for(int i=0;i<q;i++)
90+
{
91+
cin>>search;
92+
result=complete(search,head);
93+
if(result==0)
94+
{
95+
cout<<"No suggestions"<<endl;
96+
insert(search,head);
97+
}
98+
}
99+
100+
return 0;
101+
}

0 commit comments

Comments
 (0)