Medium
Write a function that reverses characters in (possibly nested) parentheses in the input string.
Input strings will always be well-formed with matching ()s.
Example
- For inputString = "(bar)", the output should be
reverseInParentheses(inputString) = "rab"; - For inputString = "foo(bar)baz", the output should be
reverseInParentheses(inputString) = "foorabbaz"; - For inputString = "foo(bar)baz(blim)", the output should be
reverseInParentheses(inputString) = "foorabbazmilb"; - For inputString = "foo(bar(baz))blim", the output should be
reverseInParentheses(inputString) = "foobazrabblim".
Because "foo(bar(baz))blim" becomes "foo(barzab)blim" and then "foobazrabblim".
Input/Output
-
[execution time limit] 0.5 seconds (c)
-
[input] string inputString
A string consisting of lowercase English letters and the characters ( and ). It is guaranteed that all parentheses in inputString form a regular bracket sequence.
Guaranteed constraints:
0 ≤ inputString.length ≤ 50. -
[output] string
- Return inputString, with all the characters that were in parentheses reversed.
[C] Syntax Tips
// Prints help message to the console
// Returns a string
char * helloWorld(char * name) {
char * answer = malloc(strlen(name) + 8);
printf("This prints to the console when you Run Tests");
strcpy(answer, "Hello, ");
strcat(answer, name);
return answer;
}
더보기
Solution
char *reverseInParentheses(char *inputString)
{
int N=strlen(inputString);
for(int i=0;i<N;i++)
if(inputString[i]==')')
{
int j, k, l;
if(i==N-1)
inputString[i]='\0';
else
{
for(j=i;j<N-1;j++)
inputString[j]=inputString[j+1];
inputString[j]='\0';
}
N--;
for(j=i-1;inputString[j]!='(';j--);
for(k=j;k<N-1;k++)
inputString[k]=inputString[k+1];
inputString[k]='\0';
N--;
i-=2;
l=(i+j)/2;
while(j<=l)
{
char temp=inputString[i];
inputString[i--]=inputString[j];
inputString[j++]=temp;
}
}
return inputString;
}
728x90
'Codesignal' 카테고리의 다른 글
<Codesignal> Add Border (0) | 2020.04.05 |
---|---|
<Codesignal> alternatingSums (0) | 2020.04.05 |
<Codesignal> Sort by Height (0) | 2020.04.05 |
<Codesignal> isLucky (0) | 2020.04.05 |
<Codesignal> commonCharacterCount (0) | 2020.04.05 |