J.C. Bose University of Science and Technology, YMCA 2024
My Stats
EXP gained
34578
Level
7 (Expert)
Community stats
Discussions
0
2
Know more
485
Total problems solved
281
Easy
173
Moderate
31
Hard
0
Ninja

Current streak:

0 days

Longest streak:

10 days

Less

More

Achievements
4
Ronin
Topics
Binary Search Trees
Trees
+ 2 more
7
Samurai
Topics
Arrays
Stacks & Queues
Strings
+ 4 more
Discussions
Shortest C++ solution ( without recursion )
``````int findCeil(BinaryTreeNode<int> *node, int x)
{
int ans = -1;

while (node) {
if(node->data < x)
node = node->right;
else{
ans = node->data;
node = node->left;
}
}

return ans;
}``````
Rahul Sharma
Published On 14-Jul-2023
232 views
0 replies
KMP - Algorithm ( C++ )
Interview problems

Time Complexity : O(n + m)

Auxiliary space : O(m)

``````vector<int> stringMatch(string text, string pattern) {

int n = text.length(), m = pattern.length();
vector<int>ans, LPS(m, 0);
int len = 0;
for(int i = 1; i < m; i++){
if(pattern[i] == pattern[len]){
len++;
LPS[i] = len;
}
else{
if(len != 0){
len = LPS[len-1];
}
}
}

int i = 0, j = 0;

while(i < n){
if(text[i] == pattern[j]){
i++;
j++;
if(j == m){
ans.push_back(i-j+1);
j = LPS[j-1];
}
}
else {
if(j > 0) j = LPS[j-1];
else i++;
}
}
return ans;
}
``````
Rahul Sharma
Published On 10-Jul-2023
459 views
0 replies