Problem Description
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.
Example 1:
Input: words =["cat","bt","hat","tree"], chars ="atach" Output: 6 Explanation: The strings that can be formed are "cat" and "hat" so the answer is 3 + 3 = 6.
Example 2:
Input: words =["hello","world","leetcode"], chars ="welldonehoneyr"Output: 10 Explanation: The strings that can be formed are "hello" and "world" so the answer is 5 + 5 = 10.
Note:
1 <= words.length <= 1000
1 <= words[i].length, chars.length <= 100
All strings contain lowercase English letters only.
解题心得(Python 3):
1. for loop / for else loop / if else loop. For loops also have an else clause which most of us are unfamiliar with. The else clause executes after the loop completes normally. This means that the loop did not encounter a break statement.
2. list.remove(x). Remember that string does not have a remove method, convert to list first. Remove the first item from the list whose value is equal to x. It raises a ValueError if there is no such item.
3. Read the description carefully, it may give you a hint. See these highlights above.
Ans.
解题交流的时候推荐用MS Teams或者Zoom网页版,可以分享屏幕和远程控制。
求轻拍,小白一枚。