当前位置: 首页 > article >正文

LeetCode467. Unique Substrings in Wraparound String——动态规划

文章目录

    • 一、题目
    • 二、题解

一、题目

We define the string base to be the infinite wraparound string of “abcdefghijklmnopqrstuvwxyz”, so base will look like this:

“…zabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd…”.
Given a string s, return the number of unique non-empty substrings of s are present in base.

Example 1:

Input: s = “a”
Output: 1
Explanation: Only the substring “a” of s is in base.
Example 2:

Input: s = “cac”
Output: 2
Explanation: There are two substrings (“a”, “c”) of s in base.
Example 3:

Input: s = “zab”
Output: 6
Explanation: There are six substrings (“z”, “a”, “b”, “za”, “ab”, and “zab”) of s in base.

Constraints:

1 <= s.length <= 105
s consists of lowercase English letters.

二、题解

class Solution {
public:
    int findSubstringInWraproundString(string s) {
        int n = s.size();
        vector<int> a(n,0);
        for(int i = 0;i < n;i++){
            a[i] = s[i] - 'a';
        }
        //dp[0]代表s中以a结尾的子串,最大延伸长度是多少(根据base串规则)
        vector<int> dp(26,0);
        dp[a[0]] = 1;
        int len = 1;
        for(int i = 1;i < n;i++){
            int cur = a[i],pre = a[i-1];
            if((pre == 25 && cur == 0) || pre == cur - 1) len++;
            else len = 1;
            dp[cur] = max(dp[cur],len);
        }
        int res = 0;
        for(int i = 0;i < 26;i++){
            res += dp[i];
        }
        return res;
    }
};

http://www.kler.cn/news/233899.html

相关文章:

  • 图形学:Transform矩阵(3维 2维) 平移,旋转,缩放
  • 力扣738单调递增的数字思路以及贪心总结
  • centos 7.6 安装 openldap 2.5.17
  • Spring基础 - SpringMVC请求流程和案例
  • 图神经网络与图表示学习: 从基础概念到前沿技术
  • 【Linux】POSIX信号量基于环形队列的生产消费模型
  • Go基础知识学习-习题题解
  • 2024年度十余爆款爱心表白代码,还不进来瞅瞅?(一)
  • Git的基础操作指令
  • java大数据hadoop2.9.2 Flume安装操作
  • Jupyter Notebook如何在E盘打开
  • 机器学习系列——(十八)K-means聚类
  • Vue-56、Vue技术路由的使用
  • 【大数据面试题】005 谈一谈 Flink Watermark 水印
  • 突破编程_C++_面试(基础知识(9))
  • 飞书上传图片
  • FPS游戏框架漫谈第二十天
  • 【大厂AI课学习笔记】【1.5 AI技术领域】(10)对话系统
  • C++ 位运算
  • Github 2024-02-06 开源项目日报Top9
  • 波奇学Linux:文件重定向和虚拟文件系统
  • 2024年 前端JavaScript入门到精通 第一天
  • 2-8 单链表+双链表+模拟栈+模拟队列
  • [Angular 基础] - 指令(directives)
  • Go语言每日一题——链表篇(七)
  • ANSI Escape Sequence 下落的方块
  • Stable Diffusion 模型下载:ToonYou(平涂卡通)
  • python实现k路归并排序
  • 在gtkmm4 中检索子控件 (children)
  • 人工智能|深度学习——使用多层级注意力机制和keras实现问题分类