cs: add «1255. Maximum Score Words Formed by Letters»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
17095ea3b2
commit
86683503c8
1 changed files with 61 additions and 0 deletions
61
cs/maximum-score-words-formed-by-letters.cs
Normal file
61
cs/maximum-score-words-formed-by-letters.cs
Normal file
|
@ -0,0 +1,61 @@
|
|||
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;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue