Given a List of words, return the words that can be typed using letters of alphabet on only one row's of American keyboard like the image below.
int insameline(char *str, int *hash)
{
for (int i = 1; str[i] != '\0'; i++)
{
if (hash[tolower(str[i - 1]) - 'a'] != hash[tolower(str[i]) - 'a'])
return 0;
}
/*just one character also return 1*/
return 1;
}
char** findWords(char** words, int wordsSize, int* returnSize)
{
int cnt = 0;
/*0 represent line 2,-1 for line 3,1 for line 1*/
int hash[26] = { 0,-1,-1,0,1,0,0,0,1,0,0,0,-1,-1,1,1,1,1,0,1,1,-1,1,-1,1,-1 };
/*malloc wordsSize pointer*/
char **res = (char **)malloc(sizeof(char *) * wordsSize);
for(int i = 0;i < wordsSize;i++)
{
if(insameline(words[i],&hash) == 1)
{
res[cnt++] = words[i];
}
}
*returnSize = cnt;
return res;
}