Leetcode #1255: Maximum Score Words Formed by Letters
In this guide, we solve Leetcode #1255 Maximum Score Words Formed by Letters in Python and focus on the core idea that makes the solution efficient.
You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Problem Statement
Given a list of words, list of single letters (might be repeating) and score 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).
Quick Facts
- Difficulty: Hard
- Premium: No
- Tags: Bit Manipulation, Array, Hash Table, String, Dynamic Programming, Backtracking, Bitmask, Counting
Intuition
Fast membership checks and value lookups are the heart of this problem, which makes a hash map the natural choice.
By storing what we have already seen (or counts/indexes), we can answer the question in one pass without backtracking.
Approach
Scan the input once, using the map to detect when the condition is satisfied and to update state as you go.
This keeps the solution linear while remaining easy to explain in an interview setting.
Steps:
- Initialize a hash map for seen items or counts.
- Iterate through the input, querying/updating the map.
- Return the first valid result or the final computed value.
Example
Input: 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]
Output: 23
Explanation:
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.
Python Solution
class Solution:
def maxScoreWords(
self, words: List[str], letters: List[str], score: List[int]
) -> int:
cnt = Counter(letters)
n = len(words)
ans = 0
for i in range(1 << n):
cur = Counter(''.join([words[j] for j in range(n) if i >> j & 1]))
if all(v <= cnt[c] for c, v in cur.items()):
t = sum(v * score[ord(c) - ord('a')] for c, v in cur.items())
ans = max(ans, t)
return ans
Complexity
The time complexity is , and the space complexity is . The space complexity is .
Edge Cases and Pitfalls
Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.
Summary
This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.