-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1190.c
More file actions
38 lines (33 loc) · 752 Bytes
/
Copy path1190.c
File metadata and controls
38 lines (33 loc) · 752 Bytes
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
// Reverse substrings between each pair of parentheses
// MEDIUM
#include <stdio.h>
#include <stdlib.h>
void reverse (char* s, int l, int r) {
while (l < r) {
char temp = s[l];
s[l] = s[r];
s[r] = temp;
l ++;
r --;
}
}
char* reverseParentheses(char* s) {
int length = strlen(s);
int* array = (int*)malloc((length + 1) * sizeof(int));
char* string = (char*)malloc((length + 2) * sizeof(char));
int top = -1;
int pos = 0;
for (int i = 0; s[i] != '\0'; i ++) {
if (s[i] == '(') {
array[++top] = pos;
} else if (s[i] == ')') {
int temp = array[top];
top --;
reverse(string, temp, pos - 1);
} else {
string[pos ++] = s[i];
}
}
string[pos] = '\0';
return string;
}