We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
/*
* Complete the 'solve' function below.
*
* The function is expected to return a DOUBLE.
* The function accepts STRING s as parameter.
/
bool is_palindrome(const char s){
int n=strlen(s);
for(int i=0;i
int min_swaps_to_palindrome(char* s){
int n=strlen(s);
int swaps=0;
for(int i=0;i<n/2;++i) {
int left = i;
int right = n-left-1;
while(left<right && s[left] != s[right]){
right--;
}
if (left==right) {
if(n % 2== 1) {
for (int j=right;j<n-left-1;++j) {
char temp = s[j];
s[j]=s[j+1];
s[j+1]=temp;
swaps++;
} continue;
} else {
return -1;
}
} else {
for (int j=right;j<n-left-1;++j) {
char temp =s[j];
s[j]=s[j+1];
s[j+1]=temp;
swaps++;
}
}
}
return swaps;
Palindromes
You are viewing a single comment's thread. Return to all comments →
include
include
include
include
include
include
include
include
include
include
char* readline(); char* ltrim(char*); char* rtrim(char*);
int parse_int(char*);
/* * Complete the 'solve' function below. * * The function is expected to return a DOUBLE. * The function accepts STRING s as parameter. / bool is_palindrome(const char s){ int n=strlen(s); for(int i=0;i
int min_swaps_to_palindrome(char* s){ int n=strlen(s); int swaps=0;
}
double solve(char* s) { if(is_palindrome(s)) { return 0.0; } int min_swaps=min_swaps_to_palindrome(s); if(min_swaps == -1) { return (double)INT_MAX; } return(double)min_swaps; }
int main() { FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w");
}
char* readline() { size_t alloc_length = 1024; size_t data_length = 0;
}
char* ltrim(char* str) { if (!str) { return '\0'; }
}
char* rtrim(char* str) { if (!str) { return '\0'; }
}
int parse_int(char* str) { char* endptr; int value = strtol(str, &endptr, 10);
} where is the issue