-
Notifications
You must be signed in to change notification settings - Fork 0
/
1021 Remove Outermost Paranthesis.c
93 lines (74 loc) · 1.74 KB
/
1021 Remove Outermost Paranthesis.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#define STACK_SIZE 10000
typedef struct stack
{
char arr[STACK_SIZE];
int top;
}Stack;
int isStackEmpty(Stack * par_stack)
{
if(par_stack->top == -1)
return 1;
else
return 0;
}
int isStackFull(Stack * par_stack)
{
if(par_stack->top == STACK_SIZE - 1)
return 1;
else
return 0;
}
void push(Stack * par_stack, char element)
{
if(isStackFull(par_stack))
return;
par_stack->arr[++(par_stack->top)] = element;
}
char pop(Stack * par_stack)
{
if(isStackEmpty(par_stack))
return;
char ret_element = par_stack->arr[par_stack->top];
(par_stack->top)--;
return ret_element;
}
char * removeOuterParentheses(char * s)
{
if(s == "")
return s;
Stack par_stack;
par_stack.top = -1;
memset(&par_stack.arr, '\0', sizeof(par_stack.arr));
char * ret_str = (char *)malloc(sizeof(char)*STACK_SIZE);
int ret_str_index = 0;
int i=0;
while(s[i]!='\0')
{
if(i==0)
{
push(&par_stack.arr, s[i]);
}
else
{
if(s[i] == '(')
{
if(!isStackEmpty(&par_stack.arr))
{
ret_str[ret_str_index++] = s[i];
}
push(&par_stack.arr, s[i]);
}
else if(s[i] == ')')
{
char pop_element = pop(&par_stack.arr);
if(!isStackEmpty(&par_stack.arr))
{
ret_str[ret_str_index++] = s[i];
}
}
}
i++;
}
ret_str[ret_str_index] = '\0';
return ret_str;
}