๐๐ฟ๐ฟ๐ฎ๐- ๐ค๐ฒ ๐ ๐ฎ๐ ๐ถ๐บ๐๐บ ๐ฆ๐ฐ๐ผ๐ฟ๐ฒ ๐ช๐ผ๐ฟ๐ฑ๐ ๐๐ผ๐ฟ๐บ๐ฒ๐ฑ ๐ฏ๐ ๐๐ฒ๐๐๐ฒ๐ฟ๐
Pranjal Sailwal
Posted on May 26, 2024
class Solution {
public int maxScoreWords(String[] words, char[] letters, int[] score) {
int[] available = new int[26];
for (char c : letters) {
available[c - 'a']++;
}
int n = words.length;
int[] wordScores = new int[n];
int[][] wordCounts = new int[n][26];
for (int i = 0; i < n; i++) {
int wordScore = 0;
for (char ch : words[i].toCharArray()) {
wordCounts[i][ch - 'a']++;
wordScore += score[ch - 'a'];
}
wordScores[i] = wordScore;
}
int maxScore = 0;
for (int mask = 0; mask < (1 << n); mask++) {
int[] totalAvailable = available.clone();
int currentScore = 0;
boolean valid = true;
for (int i = 0; i < n; i++) {
if ((mask & (1 << i)) != 0) {
for (int j = 0; j < 26; j++) {
totalAvailable[j] -= wordCounts[i][j];
currentScore += wordCounts[i][j] * score[j];
}
}
}
for (int count : totalAvailable) {
if (count < 0) {
valid = false;
break;
}
}
if (valid) {
maxScore = Math.max(maxScore, currentScore);
}
}
return maxScore;
}
}
๐ข๐ฝ๐ฒ๐ป ๐๐ผ ๐จ๐ฝ๐ฑ๐ฎ๐๐ฒ๐ ๐ฎ๐ป๐ฑ ๐ฆ๐๐ด๐ด๐ฒ๐๐๐ถ๐ผ๐ป๐.
๐ ๐ช ๐
๐ฉ
Pranjal Sailwal
Posted on May 26, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
career Burnout, Imposter Syndrome & More: What Junior Devs Really Experience ๐ต๏ธโโ๏ธ
November 28, 2024