题目:
解析:
代码:
javapublic 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; }
动态规划子数组系列一>环绕字符串中唯一的子字符串
robin_suli2024-12-08 12:43
相关推荐
思成Codes12 小时前
数据结构: 权值线段树——线段树系列(提供模板)历程里程碑12 小时前
破解三数之和:双指针高效解法Vect__12 小时前
25.12.27 算法日记——双指针Swizard12 小时前
数据不够代码凑?用 Albumentations 让你的 AI 模型“看”得更广,训练快 10 倍!一个专注写代码的程序媛12 小时前
流式读取数据Halo_tjn12 小时前
Java Set集合知识点小园子的小菜12 小时前
深入理解Trie树:敏感词过滤的核心原理与实现思路Tisfy12 小时前
LeetCode 2402.会议室 III:优先队列大模拟byzh_rc12 小时前
[算法设计与分析-从入门到入土] 基础算法2022.11.7始学前端12 小时前
Dify第二节:AI 出题助手并写入飞书云文档
