Amazon Interview Question
Software Engineer / Developersa) push when u encouter (
b)pop when u encounter )
c)It means before evry push the stack shud be empty else return false ..
@whataheck - Your would not handle case like '((()))'...it is good for case '()()()' only
Assumption: The string ONLY contains '(' and ')'.
bool check_valid_or_not( string* s)
{
int counter_for_left_paren = 0;
int counter_for_right_paren = 0;
for(int i = 0; i< s.length(); i++)
{
if(s[i] == '(') counter_for_left_paren++;
else counter_for_right_paren++;
if( counter_for_right_paren > counter_for_left_paren)
return false;
}
return (counter_for_left_paren==counter_for_right_paren);
}
Time Complexity:O(n)
Space complexity:O(1)
If anything is wrong, feel free to correct me. Thanks in advance.
Hey, what about a string "()))((" .
Above string is not wellformed. I prefer to go for using stack data structure
I think the stack approach wud work because the question clearly says .. for every ")" should have a preceding "(".
Hence the case '((()))' need not be considered .Wat say ?
'preceding' does not necessarily imply that it would be 'immediately preceding' it.
so the condition to be checked is simply 'when you pop, stack should not be empty already'
Then in that case as Srik pointed out .. the below will be a wellformed string but which is not ideally .
"()))(("
not at all. think a little more, when you are trying to pop from stack for second "right bracket )" the stack would already be empty. So you return a not-well-formed string. to explain more, here is your string example:
( --> left bracket encountered so pushed in stack
) --> right bracket encountered so popped from stack
) --> stack is empty, no more popping can be done so it would be a not-well formed string and you need not see further.
public boolean isWellformedString(final String the_string) {
Deque<Boolean> stack = new ArrayDeque<Boolean>();
for (int i = 0; i < the_string.length(); i++) {
switch(the_string.charAt(i)):
case '(':
stack.push(true);
break;
case ')':
if (!stack.isEmpty()) {
stack.pop();
} else {
return false; //if stack is empty, there wasn't a preceding '('
}
break;
}
return stack.isEmpty(); //you want an empty stack when the loop is done.
}
I forgot to add, that is Time O(n) and Space O(n). I slightly different, more readable form of the first response is:
public boolean isWellformedString(final String the_string) {
int open_count = 0;
for (int i = 0; i < the_string.length(); i++) {
switch(the_string.charAt(i)):
case '(':
open_count++;
break;
case ')':
if (open_count > 0) {
open_count--;
} else {
return false;
}
break;
}
return (open_count == 0);
}
This is Time O(n) and Space O(1). Credit to the first poster for this though, this is just a bit more readable in my opinion.
#define NULL 0
struct node
{
char s;
struct node *next;
};
struct node *top=NULL;
void push(char c)
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
temp->s=c;
if(!top)
{
top=temp;
top->next=NULL;
return;
}
temp->next=top;
top=temp;
}
char pop()
{
struct node *temp;
if(!top)
return('0');
else
temp=top;
top=top->next;
return(temp->s);
}
int isempty()
{
if(!top)
return 1;
else
return 0;
}
int IswellFormed(char str[])
{
int n;
int i;
char c;
n=strlen(str);
for(i=0;i<=n-1;i++)
{
if(str[i]=='(')
push(str[i]);
if(str[i]==')')
c=pop();
if(c=='0')
{
printf("not well formed" );
return 0;
}
}
if(isempty())
{
printf("well formed");
}
else
printf("not well formed");
}
int main(void)
{
char str[100];
gets(str);
IswellFormed(str);
return 0;
}
- Anonymous June 12, 2011