File tree Expand file tree Collapse file tree 1 file changed +30
-0
lines changed
Arrays & Strings/#500 - Keyboard Row - Easy Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments