动态规划子数组系列一>环绕字符串中唯一的子字符串

题目:


解析:


代码:

java 复制代码
 public int findSubstringInWraproundString(String ss) {
        int n =  ss.length();
        int[] dp = new int[n];
        char[] s = ss.toCharArray();
        for(int i = 0; i < n; i++) dp[i] = 1;
        for(int i = 1; i < n; i++){
            if(s[i - 1] + 1 == s[i] || (s[i - 1] == 'z' && s[i] == 'a'))
                dp[i] += dp[i-1];
        }

        //去重:把dp表中的字串重复的,只留最长的字串(hash表来建立映射关系)
        int[] hash = new int[26];
        for(int i = 0; i < n; i++)
            hash[s[i]-'a'] = Math.max(hash[s[i]-'a'], dp[i]);

  
        //返回
        int ret = 0;
        for(int x : hash)
            ret += x;

        return ret;
    }
相关推荐
AlenTech21 小时前
155. 最小栈 - 力扣(LeetCode)
算法·leetcode·职场和发展
mit6.82421 小时前
正反两次扫描|单调性cut
算法
Yzzz-F21 小时前
牛客小白月赛127 E
算法
大锦终21 小时前
递归回溯综合练习
c++·算法·深度优先
Keep__Fighting1 天前
【神经网络的训练策略选取】
人工智能·深度学习·神经网络·算法
晚风吹长发1 天前
初步了解Linux中的动静态库及其制作和使用
linux·运维·服务器·数据结构·c++·后端·算法
sin_hielo1 天前
leetcode 3453(二分法)
算法
风之歌曲1 天前
c++高精度模板
c++·算法·矩阵
嵌入式进阶行者1 天前
【算法】深度优先搜索实例:华为OD机考双机位A卷- 中庸行者
c++·算法·华为od·深度优先
a3535413821 天前
参数化曲线弧长公式推导
算法