https://leetcode-cn.com/problems/number-of-wonderful-substrings/
[out of time]
class Solution {
public long wonderfulSubstrings(String word) {
int n = word.length();
Set[][] dp = new Set[n][n];
for(int i=0;i<n;i++){
for(int j=i;j<n;j++){
dp[i][j] = new HashSet();
}
}
for(int i=0;i<n;i++){
char ch = word.charAt(i);
dp[i][i].add(ch);
}
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
char ch = word.charAt(j);
Set prev = dp[i][j-1];
dp[i][j] = new HashSet(prev);
if(prev.contains(ch)){
dp[i][j].remove(ch);
}
else {
dp[i][j].add(ch);
}
}
}
int cnt = 0;
for(int i=0;i<n;i++){
for(int j=i;j<n;j++){
int cur = dp[i][j].size();
if(cur==0 || cur==1){
cnt++;
}
}
}
return cnt;
}
}
[bug]
class Solution {
public long wonderfulSubstrings(String word) {
int n = word.length();
int[][] dp = new int[n][n];
for(int i=0;i<n;i++){
char ch = word.charAt(i);
dp[i][i] = val(ch);
}
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
char ch = word.charAt(j);
if(dp[i][j-1]==-1){
dp[i][j] = val(ch);
}
else if(dp[i][j-1]>=0 && dp[i][j-1]<=9){
if(dp[i][j-1]==val(ch)){
dp[i][j] = -1;
}
else {
dp[i][j] = dp[i][j-1]*10 + val(ch);
}
}
else if(dp[i][j-1]>9){
int num1 = dp[i][j-1]/10;
int num2 = dp[i][j-1]%10;
if(val(ch)==num1 || val(ch)==num2){
dp[i][j] = dp[i][j-1]==num2?num1:num2;
}
else {
dp[i][j] = -2;
while(++j<n){
dp[i][j] = -2;
}
break;
}
}
}
}
int cnt = 0;
for(int i=0;i<n;i++){
for(int j=i;j<n;j++){
if(dp[i][j]>=-1 && dp[i][j]<=9){
cnt++;
}
}
}
return cnt;
}
int val(char ch){
return ch - 'a';
}
}