You are given two 0-indexed arrays of strings startWords and
targetWords. Each string consists of lowercase English letters only.
For each string in targetWords, check if it is possible to choose a string from startWords and perform a conversion operation on it to be equal to that from targetWords.
The conversion operation is described in the following two steps:
Append any lowercase letter that is not present in the string to its end.
For example, if the string is "abc", the letters 'd', 'e', or 'y' can be added to it, but not 'a'. If 'd' is added, the resulting string will be "abcd".
Rearrange the letters of the new string in any arbitrary order.
For example, "abcd" can be rearranged to "acbd", "bacd", "cbda", and so on. Note that it can also be rearranged to "abcd" itself.
Return _thenumber of strings in _targetWords _that can be obtained by performing the operations onany string of _startWords.
Note that you will only be verifying if the string in targetWords can be obtained from a string in startWords by performing the operations. The strings in startWordsdo not actually change during this process.
Input: startWords =["ant","act","tack"], targetWords =["tack","act","acti"]Output: 2Explanation:
- In order to form targetWords[0]="tack", we use startWords[1]="act", append 'k' to it, and rearrange "actk" to "tack".- There is no string in startWords that can be used to obtain targetWords[1]="act". Note that "act" does exist in startWords, but we **must** append one letter to the string before rearranging it.- In order to form targetWords[2]="acti", we use startWords[1]="act", append 'i' to it, and rearrange "acti" to "acti" itself.
Input: startWords =["ab","a"], targetWords =["abc","abcd"]Output: 1Explanation:
- In order to form targetWords[0]="abc", we use startWords[0]="ab", add 'c' to it, and rearrange it to "abc".- There is no string in startWords that can be used to obtain targetWords[1]="abcd".
The key idea is to represent each word as a bitmask, where each bit corresponds to a letter. For each target word, we can remove one letter at a time and check if the resulting bitmask exists in the set of start word bitmasks. This is efficient because all words have unique letters and are at most 26 characters long.
classSolution {
public:int wordCount(vector<string>& startWords, vector<string>& targetWords) {
unordered_set<int> s;
for (auto& w : startWords) {
int mask =0;
for (char c : w) mask |=1<< (c -'a');
s.insert(mask);
}
int ans =0;
for (auto& w : targetWords) {
int mask =0;
for (char c : w) mask |=1<< (c -'a');
for (char c : w) {
int m2 = mask ^ (1<< (c -'a'));
if (s.count(m2)) {
++ans;
break;
}
}
}
return ans;
}
};
classSolution {
funwordCount(startWords: Array<String>, targetWords: Array<String>): Int {
val s = mutableSetOf<Int>()
for (w in startWords) {
var mask = 0for (c in w) mask = mask or (1 shl (c - 'a'))
s.add(mask)
}
var ans = 0for (w in targetWords) {
var mask = 0for (c in w) mask = mask or (1 shl (c - 'a'))
for (c in w) {
val m2 = mask xor (1 shl (c - 'a'))
if (m2 in s) {
ans++break }
}
}
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
classSolution:
defwordCount(self, startWords: list[str], targetWords: list[str]) -> int:
s = set()
for w in startWords:
mask =0for c in w:
mask |=1<< (ord(c) - ord('a'))
s.add(mask)
ans =0for w in targetWords:
mask =0for c in w:
mask |=1<< (ord(c) - ord('a'))
for c in w:
m2 = mask ^ (1<< (ord(c) - ord('a')))
if m2 in s:
ans +=1breakreturn ans
impl Solution {
pubfnword_count(start_words: Vec<String>, target_words: Vec<String>) -> i32 {
letmut s = std::collections::HashSet::new();
for w in start_words.iter() {
letmut mask =0;
for c in w.chars() {
mask |=1<< (c asu8-b'a');
}
s.insert(mask);
}
letmut ans =0;
for w in target_words.iter() {
letmut mask =0;
for c in w.chars() {
mask |=1<< (c asu8-b'a');
}
for c in w.chars() {
let m2 = mask ^ (1<< (c asu8-b'a'));
if s.contains(&m2) {
ans +=1;
break;
}
}
}
ans
}
}