——核心思路:将字符串根据‘d’进行拆分成不同的合规串,再根据不同的合规串进行处理

——如果发现‘d’,就重新设计起点还有参数

——重点!!! 找到当前 i 索引位置有多少符合条件的字串数量

min(lastE,lastR):找到两个的最小值earlies,earlies到i位置才能保证出现了一个‘r’和一个‘e’,保证了这段位置已经是合法的了,那么earlies索引之前的字符数量也就是合法的字串


import java.util.Scanner;

/**
 * 求字符串中不包含字符“d”且包含字符“r”与“e”的字串数量
 */
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.next();
        int len = s.length();

        long res = 0;
        
        int lastR = -1;
        int lastE = -1;
        int start = -1;
        for (int i = 0; i < len; i++) {
            char c = s.charAt(i);
            if (c == 'd'){
                // 如果遇到d,则重新规划起点位置以及重置r,e字符对应的索引位置
                start = i;
                lastR = -1;
                lastE = -1;
            }else{
                if (c == 'e') lastE = i;
                if (c == 'r') lastR = i;
                
                if (lastE != -1 && lastR != -1){
                    // 当前i索引位置的字符串有几种组合方式
                    int earlies = Math.min(lastE, lastR);
                    res += earlies - start;
                }
            }
        }

        System.out.println(res);

    }
}