nagarajmailing
BAN USERbool isPalindrome(node *head)
{
if(head==NULL || head->next==NULL)return true;
node *prev=NULL, *curr=head;
while(curr->next)
{
prev=curr;
curr=curr->n;
}
//Now curr points to last node
if(head->data !=curr->data) return false;
//Remove the last node from the list
prev->next=NULL;
//Recursively check for next item from front
return isPalindrome(head->next);
}
int height(btree *node)
{
if(node == NULL)
return 0;
return max(height(node->left), height(node->right)) + 1;
}
bool isBalanced(btree *node)
{
if(node == NULL)
return true;
if(abs(height(node->left)-height(node->right))>1)
return false;
return isBalanced(node->left) && isBalanced(node->right);
}
Change the if condition as
if(a[b[i]]==1) to get the first/last Non-repeating Character
Here is C implementation
#include<stdio.h>
#include<string.h>
int main()
{
int a[256]={0};
char *b="Helloworldd";
int i;
for(i=0;i<strlen(b);++i)
a[b[i]]++;
for(i=0;i<strlen(b);++i)
if(a[b[i]]>1)
{
printf("First Repeating %c\n",b[i]);
break;
}
for(i=strlen(b)-1;i;--i)
if(a[b[i]]>1)
{
printf("Last Repeating %c\n",b[i]);
break;
}
}
Good approach!
- nagarajmailing June 18, 2011Correct me if I am wrong
Will this work?
To check whether y lies in path from x to z
let node1=leastcommonancestor(x,y)
let node2=leastcommonancestor(z,y)
if(node1==node2)
return true
else
return false
Finally found a very easy to follow O(n2) solution in this blog
technicalypto.com/2010/02/find-all-possible-palindromes-in-string.html
It also has a working code!
Your code works perfectly for binary search trees :)
if(path[0]<curSum)
{
path[1]=level-1;
path[0]=curSum;
}
In this code segment, shouldn't path[1]=level?
Else it will miss the last node
Correct me if I am wrong
Your code is easy to follow but it is not working when the tree has negative numbers.
E.g.,tree with root=5, level (1) -> 3,-7
children of 3 -> -2,4 and children of -7 -> 6,8
Max sum path is 5->3->4
But your code takes 5->6->8 which is a non-existing path
Your code won't get the second min if the first value in the array is the minimum
Here is a code to get the min and second min in O(N)
#include<limits.h>
void main()
{
int sec_min, min,n=6,i;
int a[]={1,4,3,2,6,5};
sec_min = min = INT_MAX;
for (i =0; i < n; i ++)
{
if (a[i] < min)
min = a[i];
if(a[i] < sec_min && a[i] > min)
sec_min=a[i];
}
printf("%d %d",min,sec_min);
getchar();
}
int strstrf(char *s, char *pattern)
{
int i,j,k;
for(i=0;s[i];++i)
{
for(j=i,k=0;pattern[k]!='\0' && s[j]==pattern[k];j++,k++);
if(k>0 && pattern[k]=='\0')
return i;
}
return -1;
}
Correct me if I am wrong..
Chance of A starting first is 1/2
Chance of A rolling 1 is 1/6
Total Probability that A wins is 1/2 * 1/6 = 1/12
Probability that A not winning is 1-1/12 = 11/12
Now for B, A should not have won and B must roll a 6
So its 11/12 * 1/6= 11/72
- nagarajmailing August 28, 2012