public class Solution
{
public long WonderfulSubstrings(string word)
{
long[] count = new long[1024];
count[0] = 1;
int state = 0;
long result = 0;
for (int i = 0; i < word.Length; i++)
{
state ^= 1 << (word[i] - 'a');
result += count[state];
for (char odd = 'a'; odd <= 'j'; odd++)
{
int oddState = state ^ (1 << odd - 'a');
result += count[oddState];
}
count[state]++;
}
return result;
}
}
class Solution:
def wonderfulSubstrings(self, word: str) -> int:
# For each prefix of the string, check which characters are of even frequency and which are not and represent it by a bitmask
## ten lowercase English letters => bitmask in range (0, 1024)
bitmask_freq_table = [0] * 1024
cur_bitmask = 0
bitmask_freq_table[cur_bitmask] += 1
for char in word:
cur_bitmask ^= 1 << (ord(char) - ord('a'))
bitmask_freq_table[cur_bitmask] += 1
# Find the other prefixes whose masks differs from the current prefix mask by at most one bit
count = 0
for mash1 in range (0, 1024):
count += bitmask_freq_table[mash1] * (bitmask_freq_table[mash1] - 1) // 2
for bit in range (10):
mash2 = mash1 ^ (1 << bit)
if mash1 < mash2:
count += bitmask_freq_table[mash1] * bitmask_freq_table[mash2]
return count
const charCodeOfA = 'a'.charCodeAt(0);
Object.defineProperty(String.prototype, 'code', {
get() {
return this.charCodeAt(0) - charCodeOfA;
}
});
var wonderfulSubstrings = function (word) {
const ctr = [1];
let i = 0, ans = 0;
for (const ch of word) {
i ^= 1 << ch.code;
ctr[i] ??= 0;
ans += ctr[i];
for (let b = 1; b < (1 << 10); b <<= 1) {
ans += ctr[i ^ b] ?? 0;
}
ctr[i]++;
}
return ans;
};
Nhiều nhất 1 ký tự xuất hiện với số lần lẻ ( có 2 ký tự xuất hiện với số lần lẻ thì không được tính). Trường hợp này là tất cả các ký tự xuất hiện với số lần chẵn."abab" thì wonderful kiểu gì nhỉ?
"abab" thì wonderful kiểu gì nhỉ?
Giải LC đi hỏi luyện SQL ở đâu, cho thêm cục gạch nữa4 ngày r ko có tự giải lc dc, mà hôm nay vẫn bú 50 coin streak
mấy fen cho hỏi luyện sql thì site nào ngon nhỉ,
Leetcode cũng có đấymấy fen cho hỏi luyện sql thì site nào ngon nhỉ,
hackkerank bác, leetcode sql toàn premium thôi bác4 ngày r ko có tự giải lc dc, mà hôm nay vẫn bú 50 coin streak
mấy fen cho hỏi luyện sql thì site nào ngon nhỉ,
cam on bác nhé, lc cày chay thôi nên qua hackkerank vhackkerank bác, leetcode sql toàn premium thôi bác, mà bác rich thì leetcode mà quất thôi
public long wonderfulSubstrings(String word) {
int wordLength = word.length();
int[] frequency = new int['j' - 'a' + 1];
long wordCount = 0;
int oddCount = 0;
for (int i = 0; i < wordLength; i++) {
for (int j = i; j < wordLength; j++) {
int charIdx = word.charAt(j) - 'a';
frequency[charIdx]++;
int f = frequency[charIdx];
if (f % 2 > 0) {
oddCount++;
} else {
oddCount--;
}
if (oddCount > 1) {
continue;
} else {
wordCount++;
}
}
oddCount = 0;
Arrays.fill(frequency, 0);
}
return wordCount;
}
Đợi tui luyện leetcode thạo dạng này đã rồi đứa nào phỏng vấn vào công ty tui, tui sẽ đè ra hỏimấy bài bit này interview có hỏi không nhỉ. Chứ ngán mấy dạng này quá