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

这篇具有很好参考价值的文章主要介绍了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.文章来源地址https://www.toymoban.com/news/detail-825860.html

二、题解

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;
    }
};

到了这里,关于LeetCode467. Unique Substrings in Wraparound String——动态规划的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • R语言 Error in make.names(col.names, unique = TRUE) : invalid multibyte string at ‘<b1><ea><cc><e2>‘

    R语言导入CSV文件的时候,代码如下: 出现以下报错: Error in make.names(col.names, unique = TRUE) : invalid multibyte string at \\\'b1eacce2\\\' 报错的解决方法如下: 报错的原因是,导入文件的编码格式不是read.csv()函数的默认格式。我们可以使用windows自带的“记事本/notepad”软件来查看格式,打

    2024年02月02日
    浏览(39)
  • LeetCode --- 1903. Largest Odd Number in String 解题报告

    You are given a string  num , representing a large integer. Return  the  largest-valued odd  integer (as a string) that is a  non-empty substring  of  num , or an empty string  \\\"\\\"  if no odd integer exists . A  substring  is a contiguous sequence of characters within a string. Example 1: Example 2: Example 3:

    2024年02月10日
    浏览(23)
  • LeetCode 833. Find And Replace in String【字符串,哈希表,模拟】1460

    本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,

    2024年02月12日
    浏览(32)
  • LeetCode练习七:动态规划上:线性动态规划

    参考《OI Wiki动态规划》、《算法通关手册》动态规划篇 1.1 动态规划简介    动态规划(Dynamic Programming) :简称 DP ,是一种通过把原问题分解为相对简单的子问题的方式而求解复杂问题的方法。 动态规划方法与分治算法类似,却又不同于分治算法。 「动态规划的核心思想

    2024年02月12日
    浏览(58)
  • LeetCode 2496. Maximum Value of a String in an Array【字符串,数组】简单

    本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,

    2024年02月11日
    浏览(40)
  • leetcode 动态规划(单词拆分)

    139.单词拆分 力扣题目链接(opens new window) 给定一个非空字符串 s 和一个包含非空单词的列表 wordDict,判定 s 是否可以被空格拆分为一个或多个在字典中出现的单词。 说明: 拆分时可以重复使用字典中的单词。 你可以假设字典中没有重复的单词。 示例 1: 输入: s = “leetcode”

    2024年02月22日
    浏览(32)
  • Leetcode 刷题 动态规划

    309. 最佳买卖股票时机含冷冻期 1. 确定dp数组以及下标的含义 具体可以区分出如下四个状态: 状态一:持有股票状态(今天买入股票,或者是之前就买入了股票然后没有操作,一直持有) 不持有股票状态,这里就有两种卖出股票状态 状态二:保持卖出股票的状态(两天前就

    2024年02月11日
    浏览(32)
  • leetcode-动态规划【背包问题】

    基础背包: 416. 分割等和子集 1049. 最后一块石头的重量ii 494. 目标和 474. 一和零 完全背包: 518. 零钱兑换ii 377. 组合总和iv 70. 爬楼梯 322. 零钱兑换 279. 完全平方数 139. 单词拆分 多重背包: n件物品和最大承受重量为w的背包,其中第i件物品的重量是weight[i],得到的价值是val

    2023年04月08日
    浏览(24)
  • 【LeetCode】--- 动态规划 集训(一)

    题目地址: 1137. 第 N 个泰波那契数 泰波那契序列 Tn 定义如下: T0 = 0, T1 = 1, T2 = 1 , 且在 n = 0 的条件下 Tn+3 = Tn + Tn+1 + Tn+2 给你整数 n ,请返回第 n 个泰波那契数 Tn 的值。 示例 1: 输入:n = 4 输出:4 解释: T_3 = 0 + 1 + 1 = 2 T_4 = 1 + 1 + 2 = 4 示例 2: 输入:n = 25 输出:1389537 因为

    2024年03月25日
    浏览(40)
  • LeetCode——动态规划篇(一)

    刷题顺序及思路来源于代码随想录,网站地址:https://programmercarl.com  目录 509. 斐波那契数 - 力扣(LeetCode) 70. 爬楼梯 - 力扣(LeetCode) 746. 使用最小花费爬楼梯 - 力扣(LeetCode) 62. 不同路径 - 力扣(LeetCode)  63. 不同路径 II - 力扣(LeetCode) 斐波那契数  (通常用  F(n)  表

    2024年02月09日
    浏览(26)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包