Directi Interview Question
Software Engineer / DevelopersCountry: India
Interview Type: Written Test
#include "stdafx.h"
#include <string>
#include <vector>
#include <iostream>
using namespace std;
int check(string path, string A)
{
if (A.length() > path.length())
return 0;
int i,j=0;
for (i = 0; i < path.length(); i++)
{
if (A[j] == path[i])
j++;
if (j == A.length())
return 1;
}
return 0;
}
int explode_paths(int N, string S, int K, string A)
{
int i, j, count = 0, c = 0;
string path;
vector<int> child_count;
for (i = N - 1; i >= 0; i--)
{
path.push_back(S[i]);
if (islower(S[i]))
{
cout << path << endl;
c++;
if (check(path, A))
count++;
path.pop_back();
child_count.back()++;
}
else
child_count.push_back(0);
while (!child_count.empty() && child_count.back() == 2)
{
path.pop_back();
child_count.pop_back();
if (!child_count.empty())
child_count.back()++;
}
}
return count;
}
int _tmain(int argc, _TCHAR* argv[])
{
int N, K, res;
string S, A;
N = 9;
S = "txyBabABA";
K = 2;
A = "AA";
cout << "Paths:" << endl << endl;
res = explode_paths(N, S, K, A);
cout << endl <<"No of times "<< A <<" occurs: "<< res << endl;
return res;
}
import java.util.Stack;
class Node
{
Node(char c)
{
this.c=c;
}
char c;
Node left,right;
}
class TreeProb
{
int z=0;
int explodePaths(int n, String s, int k, String a)
{
Node root=createBinary(s,n);
check(root,a);
return z;
}
void check(Node n, String s)
{
if(s.length()==0)
{
if((int)n.c>91)z++;
else
{
if(n.left!=null)check(n.left,s);
if(n.right!=null)check(n.right,s);
}
}
else
{
if(n.c==s.charAt(0))s=s.substring(1);
if(n.left!=null)check(n.left,s);
if(n.right!=null)check(n.right,s);
}
}
Node createBinary(String s, int n)
{
Stack<Node> s1 = new Stack<Node>();
Node root=null;
char c;
for(int i=0;i<n;i++)
{
Node n1=null;
if((int)(c=s.charAt(i))>91)
s1.push(new Node(c));
else
{
n1 = new Node(c);
n1.right=s1.pop();
n1.left=s1.pop();
s1.push(n1);
}
if(i==(n-1))root=n1;
}
return root;
}
}
class Tree
{
public static void main(String args[])
{
TreeProb d = new TreeProb();
System.out.println(d.explodePaths(15,"lgxBdeCAYkftKZX",1,"A"));
}
}
import java.util.Stack;
class Node
{
Node(char c)
{
this.c=c;
}
char c;
Node left,right;
}
class TreeProb
{
int z=0;
int explodePaths(int n, String s, int k, String a)
{
Node root=createBinary(s,n);
check(root,a);
return z;
}
void check(Node n, String s)
{
if(s.length()==0)
{
if((int)n.c>91)z++;
else
{
if(n.left!=null)check(n.left,s);
if(n.right!=null)check(n.right,s);
}
}
else
{
if(n.c==s.charAt(0))s=s.substring(1);
if(n.left!=null)check(n.left,s);
if(n.right!=null)check(n.right,s);
}
}
Node createBinary(String s, int n)
{
Stack<Node> s1 = new Stack<Node>();
Node root=null;
char c;
for(int i=0;i<n;i++)
{
Node n1=null;
if((int)(c=s.charAt(i))>91)
s1.push(new Node(c));
else
{
n1 = new Node(c);
n1.right=s1.pop();
n1.left=s1.pop();
s1.push(n1);
}
if(i==(n-1))root=n1;
}
return root;
}
}
class Tree
{
public static void main(String args[])
{
TreeProb d = new TreeProb();
System.out.println(d.explodePaths(15,"lgxBdeCAYkftKZX",1,"A"));
}
}import java.util.Stack;
class Node
{
Node(char c)
{
this.c=c;
}
char c;
Node left,right;
}
class TreeProb
{
int z=0;
int explodePaths(int n, String s, int k, String a)
{
Node root=createBinary(s,n);
check(root,a);
return z;
}
void check(Node n, String s)
{
if(s.length()==0)
{
if((int)n.c>91)z++;
else
{
if(n.left!=null)check(n.left,s);
if(n.right!=null)check(n.right,s);
}
}
else
{
if(n.c==s.charAt(0))s=s.substring(1);
if(n.left!=null)check(n.left,s);
if(n.right!=null)check(n.right,s);
}
}
Node createBinary(String s, int n)
{
Stack<Node> s1 = new Stack<Node>();
Node root=null;
char c;
for(int i=0;i<n;i++)
{
Node n1=null;
if((int)(c=s.charAt(i))>91)
s1.push(new Node(c));
else
{
n1 = new Node(c);
n1.right=s1.pop();
n1.left=s1.pop();
s1.push(n1);
}
if(i==(n-1))root=n1;
}
return root;
}
}
class Tree
{
public static void main(String args[])
{
TreeProb d = new TreeProb();
System.out.println(d.explodePaths(15,"lgxBdeCAYkftKZX",1,"A"));
}
}
import java.util.Stack;
class Node
{
Node(char c)
{
this.c=c;
}
char c;
Node left,right;
}
class TreeProb
{
int z=0;
int explodePaths(int n, String s, int k, String a)
{
Node root=createBinary(s,n);
check(root,a);
return z;
}
void check(Node n, String s)
{
if(s.length()==0)
{
if((int)n.c>;91)z++;
else
{
if(n.left!=null)check(n.left,s);
if(n.right!=null)check(n.right,s);
}
}
else
{
if(n.c==s.charAt(0))s=s.substring(1);
if(n.left!=null)check(n.left,s);
if(n.right!=null)check(n.right,s);
}
}
Node createBinary(String s, int n)
{
Stack<Node> s1 = new Stack<Node>();
Node root=null;
char c;
for(int i=0;i<n;i++)
{
Node n1=null;
if((int)(c=s.charAt(i))>;91)
s1.push(new Node(c));
else
{
n1 = new Node(c);
n1.right=s1.pop();
n1.left=s1.pop();
s1.push(n1);
}
if(i==(n-1))root=n1;
}
return root;
}
}
class Tree
{
public static void main(String args[])
{
TreeProb d = new TreeProb();
System.out.println(d.explodePaths(15,"lgxBdeCAYkftKZX",1,"A"));
}
}
#include<iostream>
using namespace std;
struct node
{
char val;
node *lc;
node *rc;
}*root;
void setLeft(node *a,char c)
{
node *temp=new node();
temp->val=c;
temp->lc=NULL;
temp->rc=NULL;
a->lc=temp;
}
void setRight(node *a,char c)
{
node *temp=new node();
temp->val=c;
temp->lc=NULL;
temp->rc=NULL;
a->rc=temp;
}
void buildTree(node *root,string s,int& len)
{
if(root->val>=65&&root->val<=90)
{
len--;
setRight(root,s[len]);
buildTree(root->rc,s,len);
len--;
setLeft(root,s[len]);
buildTree(root->lc,s,len);
}
}
void checkLeaf(node *a,int &count)
{
if(a->val>=97&&a->val<=122)
{
count++;
return;
}
checkLeaf(a->lc,count);
checkLeaf(a->rc,count);
}
void explorePath(node *root,string a,int pos,int &count,int k)
{
if(root==NULL)
return;
if(pos==k-1)
{
if(root->val!=a[pos])
{
explorePath(root->lc,a,pos,count,k);
explorePath(root->rc,a,pos,count,k);
}
if(root->val==a[pos]&&(root->val>=97&&root->val<=122))
{
count++;
return;
}
else if(root->val==a[pos]&&(root->val>=65&&root->val<=90))
{
checkLeaf(root->lc,count);
checkLeaf(root->rc,count);
return;
}
}
else
{if(root->val==a[pos])
pos++;
explorePath(root->lc,a,pos,count,k);
explorePath(root->rc,a,pos,count,k);
}
}
int main()
{
int n,k;
string s,a;
cin>>n>>s>>k>>a;
n--;
root=new node();
root->val=s[n];
root->lc=root->rc=NULL;
buildTree(root,s,n);
int count=0;
explorePath(root,a,0,count,k);
cout<<count;
return 0;
}
- arpitbaheti7 October 12, 2013