-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
1255. Maximum Score Words Formed by Letters #264
Comments
The problem is about maximizing the score of valid words formed using a given set of letters. The words can only be used once, and each letter in the list can be used at most as many times as it appears in the list. The score for each word is determined by the sum of the scores of its individual characters. We aim to find the maximum score obtainable from any combination of valid words formed. Key Points
Approach
Plan
Let's implement this solution in PHP: 1255. Maximum Score Words Formed by Letters <?php
/**
* @param String[] $words
* @param String[] $letters
* @param Integer[] $score
* @return Integer
*/
function maxScoreWords($words, $letters, $score) {
$letterCounts = array_fill(0, 26, 0); // Array to hold counts of each letter in 'letters'
// Count the occurrence of each letter in 'letters'
foreach ($letters as $letter) {
$letterCounts[ord($letter) - ord('a')]++;
}
$numWords = count($words); // Total number of words
$maxScore = 0; // Variable to store the maximum score
// Loop through all combinations of words
for ($combination = 0; $combination < (1 << $numWords); ++$combination) {
$wordCounts = array_fill(0, 26, 0); // Current letter counts for the current combination of words
// Build the count for the current combination
for ($wordIndex = 0; $wordIndex < $numWords; ++$wordIndex) {
// Check if the word at wordIndex is included in the current combination
if ($combination >> $wordIndex & 1) {
// If so, count each letter in the word
foreach (str_split($words[$wordIndex]) as $letter) {
$wordCounts[ord($letter) - ord('a')]++;
}
}
}
$isValidCombination = true; // Flag to check if the combination is valid
$currentScore = 0; // Score for the current combination
for ($letterIndex = 0; $letterIndex < 26; ++$letterIndex) {
// If any letter count exceeds what is available, the combination is invalid
if ($wordCounts[$letterIndex] > $letterCounts[$letterIndex]) {
$isValidCombination = false;
break; // No need to continue if the combination is invalid
}
// Calculate score for the current combination
$currentScore += $wordCounts[$letterIndex] * $score[$letterIndex];
}
// If the combination is valid and the score is higher than the maximum found so far, update maxScore
if ($isValidCombination && $maxScore < $currentScore) {
$maxScore = $currentScore;
}
}
// Return the maximum score possible with the given words and letters
return $maxScore;
}
// Example usage:
$words = ["dog","cat","dad","good"];
$letters = ["a","a","c","d","d","d","g","o","o"];
$score = [1,0,9,5,0,0,3,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0];
echo maxScoreWords($words, $letters, $score) . "\n"; // Output: 23
$words = ["xxxz","ax","bx","cx"];
$letters = ["z","a","b","c","x","x","x"];
$score = [4,4,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,0,10];
echo maxScoreWords($words, $letters, $score) . "\n"; // Output: 27
$words = ["leetcode"];
$letters = ["l","e","t","c","o","d"];
$score = [0,0,1,1,1,0,0,0,0,0,0,1,0,0,1,0,0,0,0,1,0,0,0,0,0,0];
echo maxScoreWords($words, $letters, $score) . "\n"; // Output: 0
?> Explanation:
Example WalkthroughExample 1:
Time Complexity
Output for Example 1maxScoreWords(["dog", "cat", "dad", "good"], ["a", "a", "c", "d", "d", "d", "g", "o", "o"], [1,0,9,5,0,0,3,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0]) Output:
This approach efficiently computes the maximum score of any valid combination of words using bitmasking and dynamic checking of letter counts. The use of subsets ensures that we examine all possible combinations of words, while the letter counting ensures we only consider valid word formations. |
Discussed in #263
Originally posted by mah-shamim August 9, 2024
Given a list of
words
, list of singleletters
(might be repeating) andscore
of every character.Return the maximum score of any valid set of words formed by using the given letters (
words[i]
cannot be used two or more times).It is not necessary to use all characters in
letters
and each letter can only be used once. Score of letters'a'
,'b'
,'c'
,...
,'z'
is given byscore[0]
,score[1]
,...
,score[25]
respectively.Example 1:
Score a=1, c=9, d=5, g=3, o=2
Given letters, we can form the words "dad" (5+1+5) and "good" (3+2+2+5) with a score of 23.
Words "dad" and "dog" only get a score of 21.
Example 2:
Score a=4, b=4, c=4, x=5, z=10
Given letters, we can form the words "ax" (4+5), "bx" (4+5) and "cx" (4+5) with a score of 27.
Word "xxxz" only get a score of 25.
Example 3:
Letter "e" can only be used once.
Constraints:
1 <= words.length <= 14
1 <= words[i].length <= 15
1 <= letters.length <= 100
letters[i].length == 1
score.length == 26
0 <= score[i] <= 10
words[i]
,letters[i]
contains only lower case English letters.The text was updated successfully, but these errors were encountered: