1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-11-14 01:49:41 +01:00
LeetCode/cs/maximum-score-words-formed-by-letters.cs
2024-05-25 00:55:05 +02:00

61 lines
1.4 KiB
C#

public class Solution {
private int[] freqs;
private void SetFreqs(char[] letters) {
freqs = new int[26];
foreach (char c in letters) {
++freqs[c - 'a'];
}
}
private int maxScore;
private int currentScore;
private string[] words;
private int[] score;
private void check(int i) {
if (i >= words.Length) {
maxScore = Math.Max(maxScore, currentScore);
return;
}
// Skipping the current word
check(i + 1);
// Try to add word
int j;
for (j = 0; j < words[i].Length; ++j) {
var c = words[i][j] - 'a';
--freqs[c];
currentScore += score[c];
if (freqs[c] < 0) {
break;
}
}
// Means we were able to use all letters from the word
if (j == words[i].Length) {
check(i + 1);
}
// Revert the changes
for (j = Math.Min(j, words[i].Length - 1); j >= 0; --j) {
var c = words[i][j] - 'a';
++freqs[c];
currentScore -= score[c];
}
}
public int MaxScoreWords(string[] words, char[] letters, int[] score) {
SetFreqs(letters);
maxScore = 0;
currentScore = 0;
this.words = words;
this.score = score;
check(0);
return maxScore;
}
}