So this is a problem off leetcode and I'm having some issues with it.. I've seen code that solves this problem posted to the discussion's portion of leetcode, but I was wondering if anyone could help me solve it with some of the code I've already written.. Here's what the problem is asking:
You are given a string
allowedconsisting of distinct characters and an array of stringswords. A string is consistent if all characters in the string appear in the stringallowed. Return the number of consistent strings in the array words.Example:
Input:allowed = "ab",words => ["ad","bd","aaab","baa","badab"]Output: 2
Explanation: Strings "aaab" and "baa" are consistent since they only contain characters 'a' and 'b'.
Here's my code so far:
Note: allowed contains a single string
#include <string.h>
int countConsistentStrings(char * allowed, char ** words, int wordsSize){
int real_count = 0;
for(int i = 0; i < wordsSize; i++)
{
for(int j = 0; j < strlen(allowed); j++)
{
for(int k = 0; k < strlen(words[i]); k++)
{
if(words[i][k] != allowed[j])
{
// stuff goes here?
}
}
}
}
return real_count;
}
I'm able to iterate through the strings in the array char by char and compare them to allowed's chars... But I really don't know where to go from there. I've tried keeping tallies of stuff and using comparisons but then I run into issues of whether words[i] is less than strlen(allowed) or vice-versa..
I know there's probably easier ways to do this problem (as I've seen posted on the discussion portion of leetcode) but I'm wondering if there's anyway to solve it using what I've already done?
Thank you anyone who can help me out with this... I feel like I'm "almost" there with this problem, but I've also been stuck on it quite a while so I'm ready for any explanations I can get.