c语言查找字符串中单词_查找可以由字符组成的单词

c语言查找字符串中单词

While studying for interviews, I’ve been practicing my algorithm skills on LeetCode and other sites quite a bit. Read on for a walkthrough of my JavaScript solution to the Find Words That Can Be Formed by Characters problem on LeetCode (instructions from LeetCode are below).

在学习面试时,我已经在LeetCode和其他站点上练习了很多算法技巧。 阅读我JavaScript解决方案的演练,以解决LeetCode上的“可以由字符形成的单词”问题(来自LeetCode的说明如下)。

问题 (The Problem)

You are given an array of strings words and a string chars. A string is good if it can be formed by characters from chars (each character can only be used once). Return the sum of lengths of all good strings in words.

给您一个字符串words数组和一个字符串chars 。 如果字符串可以由字符中的chars (每个字符只能使用一次),则该字符串是好的 。 返回在所有好的字符串长度的总和words

Note:

注意:

  1. 1 <= words.length <= 1000

    1 <= words.length <= 1000

  2. 1 <= words[i].length, chars.length <= 100

    1 <= words[i].length, chars.length <= 100

  3. All strings contain lowercase English letters only.

    所有字符串仅包含小写英文字母。

Example 1:

范例1:

Input: words = ["cat","bt","hat","tree"], chars = "atach"Output: 6Explanation: 
The strings that can be formed are "cat" and "hat" so the answer is 3 + 3 = 6.

Example 2:

范例2:

Input: words = ["hello","world","leetcode"], chars = "welldonehoneyr"Output: 10Explanation: 
The strings that can be formed are "hello" and "world" so the answer is 5 + 5 = 10.

The Solution

解决方案

Put simply, we’re going to create a counter variable, build a loop through the words array that creates objects from chars, then loop through each word in words to determine whether it is good or not. If it is good, we’ll add its length to our counter, and return the final count at the end. Let’s break this down!

简而言之,我们将创建一个计数器变量,通过word数组构建一个循环,该循环从chars创建对象,然后循环遍历word中的每个单词以确定它是否好。 如果好,我们将其长度添加到计数器中,并在最后返回最终计数。 让我们分解一下!

First, let’s create our counter variable — we’ll call this finalCount since we’re returning it at the end as a final count. Let’s also make a variable called charObj which will eventually hold a character object, but will not have a value for now.

首先,让我们创建计数器变量-我们将其称为finalCount,因为我们在最后将其作为最终计数返回。 我们还创建一个名为charObj的变量,该变量最终将保存一个字符对象,但暂时没有值。

Let’s move on to our outer for loop. Starting at index 0, we will loop over each word in the words array. Because each character in char can only used once when forming a good word, we know that any word that has a greater length than the length of char cannot possibly be good. We’ll save some time on those words and keep moving through the outer loop to the next word instead of spending time on our (yet to be created) inner loop when that happens.

让我们继续我们的外部for循环。 从索引0开始,我们将遍历words数组中的每个单词。 由于char中的每个字符在构成一个好的单词时只能使用一次,因此我们知道,长度大于char长度的任何单词都不可能是好的。 我们将在这些单词上节省一些时间,并继续在外循环中移动到下一个单词,而不是在发生这种情况时花时间在我们(尚未创建)的内循环上。

We’re going to want to keep track of what characters from char we’ve already compared, so we will create a new character object in the outer loop whenever we start going through a new word to ensure the count of each character in char is reset to its original state. We’ll have a separate helper function (included in our final solution) called makeCharObj which will create the character object, and assign the value to our already created charObj variable.

我们将要跟踪已经比较过的char中的哪些字符,因此,每当我们开始使用新单词以确保char中每个字符的计数为重置为其原始状态。 我们将有一个名为makeCharObj的单独的辅助函数(包含在最终解决方案中),该函数将创建字符对象,并将该值分配给我们已经创建的charObj变量。

var countCharacters = function(words, chars) {
       
let finalCount = 0;
let charObj;
for (let i = 0; i < words.length; i++) {
if (words[i].length > chars.length) continue;
charObj = makeCharObj(chars);
}
}

Let’s move onto our inner for loop! We’ll be iterating through each word of words, with our incrementing variable j starting at index 0. To make our code a little cleaner, we can create a variable called char, equal to words[i][j], or each character in each string in the array.

让我们进入内部for循环! 我们将遍历单词中的每个单词,并使用从索引0开始的递增变量j。为使我们的代码更加简洁,我们可以创建一个称为char的变量,它等于单词[i] [j]或每个字符在数组中的每个字符串中。

Now we just need a conditional statement to determine if the word is good and add it to our final count if so. If we’ve reached the end of our word and the character object has a value at the key of the current character, we can add the length of the current word to our final count, since the word has been validated as good. Otherwise, if the character object has a value at the key of the current character but we’re not at the end of the current word, we will keep going through the loop to the next letter of the word, and also decrement the count of the value of the character object’s key, since we need to track how many times each character in char is used in the word. If neither of these conditions are met, we will break out of the inner loop and move on to the next word, because it means that the character object does not have a value at the key of the current character, so it is not solely made up of characters in char, and is therefore not a good string.

现在,我们只需要一个条件语句来确定单词是否良好,如果可以,将其添加到最终计数中。 如果我们到达了单词的末尾,并且字符对象在当前字符的键处具有值,则可以将当前单词的长度添加到最终计数中,因为该单词已经过验证。 否则,如果字符对象在当前字符的键处有一个值,但我们不在当前单词的末尾,则我们将继续遍历循环到单词的下一个字母,并减少字符对象的键的值,因为我们需要跟踪char中每个字符在单词中的使用次数。 如果这两个条件均不满足,我们将跳出内循环并转到下一个单词,因为这意味着字符对象在当前字符的键处没有值,因此不是唯一的字符中的字符,因此不是一个好的字符串。

for (let j = 0; j < words[i].length; j++) {
       
let char = words[i][j];
if (j == words[i].length - 1 && charObj[char]) {
finalCount += words[i].length;
} else if (charObj[char]) {
charObj[char]--;
} else {
break;
}
}

The only thing left to do after running through both loops is to return our final count! Let’s take a look at our full solution, including our helper function, with a runtime of O(n²).

经过两个循环后,剩下要做的唯一一件事就是返回我们的最终计数! 让我们看一下完整的解决方案,包括我们的帮助函数,其运行时为O(n²)。

Problem solved! 问题解决了!

You can find the problem on LeetCode here!

你可以找到关于本文给出了这个问题在这里

Thanks for reading!

谢谢阅读!

翻译自: https://medium.com/@jenlindner22/find-words-that-can-be-formed-by-characters-6b4053e62e

c语言查找字符串中单词

    原文作者:weixin_26723981
    原文地址: https://blog.csdn.net/weixin_26723981/article/details/108495696
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞