struct custom{
int idx;
char alpha;
};
int comp(const void *a, const void *b){
struct custom *p1 = *(struct custom **)a ;
struct custom *p2 = *(struct custom **)b ;
return (p1->idx - p2->idx);
}
char* customSortString(char* order, char* s) {
int *index= calloc (26, sizeof(int));
for (int i=0; i<26; i++)
index[i] = -1;
int len_order = strlen(order);
for (int i=0 ; i<len_order; i++)
index[order[i]-'a']=i;
int len = strlen(s);
struct custom **ptr = calloc(len , sizeof(struct custom*));
for (int i=0; i<len; i++)
{
ptr[i]= calloc (1, sizeof(struct custom));
ptr[i]->idx = index[s[i]-'a'];
ptr[i]->alpha = s[i];
}
qsort((void *)ptr , len , sizeof (struct custom*), comp);
char *ret = calloc (len+1 , sizeof(char));
for (int i=0; i< len; i++)
ret[i]= ptr[i]->alpha;
return ret;
}
說是可以算s 每個字母出現多少次~
然後再回到order, 以order 的index (order 字母出現的順序)去抓s去 result ,
order 都檢查完以後~再掃一遍把不在order出現的s 送去result .
感覺某公司很喜歡這種轉兩圈的題目啊~(遠目again)
沒有留言:
張貼留言