Skip to content

Commit 0cba17a

Browse files
author
Daniel Nallapalli
committed
Solution #500 (C) - Daniel - 16/06/2025 - commit #5
1 parent 59809ef commit 0cba17a

File tree

1 file changed

+30
-0
lines changed
  • Arrays & Strings/#500 - Keyboard Row - Easy

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Note: The returned array must be malloced, assume caller calls free().
3+
*/
4+
bool isInRow(const char* row, const char* word) {
5+
for (int i = 0; word[i]; i++) {
6+
if (!strchr(row, tolower(word[i]))) {
7+
return false;
8+
}
9+
}
10+
return true;
11+
}
12+
13+
char** findWords(char** words, int wordsSize, int* returnSize) {
14+
const char* row1 = "qwertyuiop";
15+
const char* row2 = "asdfghjkl";
16+
const char* row3 = "zxcvbnm";
17+
18+
char** result = (char**)malloc(wordsSize * sizeof(char*));
19+
int count = 0;
20+
21+
for (int i = 0; i < wordsSize; i++) {
22+
char* word = words[i];
23+
if (isInRow(row1, word) || isInRow(row2, word) || isInRow(row3, word)) {
24+
result[count++] = word;
25+
}
26+
}
27+
28+
*returnSize = count;
29+
return result;
30+
}

0 commit comments

Comments
 (0)