Medium
Given a string, find the shortest possible string which can be achieved by adding characters to the end of initial string to make it a palindrome.
Example
For st = "abcdc", the output should be
buildPalindrome(st) = "abcdcba".
Input/Output
-
[execution time limit] 0.5 seconds (c)
-
[input] string st
A string consisting of lowercase English letters.
Guaranteed constraints:
3 ≤ st.length ≤ 10. -
[output] string
[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 *buildPalindrome(char *st)
{
int N=strlen(st);
for(int i=0;i<N;i++)
{
bool palindrome=true;
if(st[i]==st[N-1])
{
for(int j=1;i+j<N-1;j++)
if(st[i+j]!=st[N-j-1])
{
palindrome=false;
break;
}
if(palindrome)
{
for(int j=i-1;j>=0;j--)
st[N++]=st[j];
st[N]='\0';
return st;
}
}
}
}
728x90
'Codesignal' 카테고리의 다른 글
<Codesignal> Is MAC48 Address? (0) | 2020.04.06 |
---|---|
<Codesignal> Elections Winners (0) | 2020.04.06 |
<Codesignal> isBeautifulString (0) | 2020.04.05 |
<Codesignal> Bishop and Pawn (0) | 2020.04.05 |
<Codesignal> digitDegree (0) | 2020.04.05 |