2023年9月4日 星期一

[524] Longest Word in Dictionary through Deleting

感覺有點空虛的題目 囧
總之就是各種小地方要檢查就對了Orz

沒有什麼厲害的技巧QQ

char * findLongestWord(char * s, char ** dictionary, int dictionarySize){
int sLen = strlen (s);
int matchIdx= -1;
int maxLen = INT_MIN;
for (int i=0;i<dictionarySize;i++)
{
char *cur = dictionary[i];
int dicLen = strlen(cur);
int src=0, dic=0;
while (src < sLen && dic< dicLen)
if (s[src++] == cur[dic])
dic++;

if (dic== dicLen)
{
if (dic > maxLen)
{
maxLen = dic;
matchIdx = i;
}
else if (dic == maxLen && strcmp(dictionary[matchIdx],dictionary[i])>0)
matchIdx = i;
}
}
if (matchIdx<0)
return "";
return dictionary[matchIdx];
}

沒有留言:

張貼留言