ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

leetcode 467. Unique Substrings in Wraparound String | 467. 环绕字符串中唯一的子字符串(动态规划)

2021-09-26 17:30:25  阅读:181  来源: 互联网

标签:arr String int 467 ++ curLen 字符串 dp charAt


题目

https://leetcode.com/problems/unique-substrings-in-wraparound-string/
在这里插入图片描述

题解

1、dp 超时版本
class Solution {
    public int findSubstringInWraproundString(String p) {
        HashSet<Integer>[] arr = new HashSet[26]; // <结尾字母,长度>
        for (int i = 0; i < 26; i++) {
            arr[i] = new HashSet();
        }
        
        arr[p.charAt(0) - 'a'].add(1);
        int curLen = 1; // 避免误将断链情况计入结果
        for (int i = 1; i < p.length(); i++) {
            arr[p.charAt(i) - 'a'].add(1);
            if (p.charAt(i) == 'a' && p.charAt(i - 1) == 'z') {
                curLen++;
                for (int len : arr[25]) {
                    if (len + 1 <= curLen) arr[0].add(len + 1);
                }
            } else if (p.charAt(i - 1) == p.charAt(i) - 1) {
                curLen++;
                for (int len : arr[p.charAt(i - 1) - 'a']) {
                    if (len + 1 <= curLen) arr[p.charAt(i) - 'a'].add(len + 1);
                }
            } else {
                curLen = 1;
            }
        }
        
        int result = 0;
        for (int i = 0; i < 26; i++) {
            result += arr[i].size();
        }
        return result;
    }
}
2、dp 答案版本

参考:Concise Java solution using DP

After failed with pure math solution and time out with DFS solution, I finally realized that this is a DP problem…
The idea is, if we know the max number of unique substrings in p ends with 'a', 'b', ..., 'z', then the summary of them is the answer. Why is that?

  1. The max number of unique substring ends with a letter equals to the length of max contiguous substring ends with that letter. Example "abcd", the max number of unique substring ends with 'd' is 4, apparently they are "abcd", "bcd", "cd" and "d".
  2. If there are overlapping, we only need to consider the longest one because it covers all the possible substrings. Example: "abcdbcd", the max number of unique substring ends with 'd' is 4 and all substrings formed by the 2nd "bcd" part are covered in the 4 substrings already.
  3. No matter how long is a contiguous substring in p, it is in s since s has infinite length.
  4. Now we know the max number of unique substrings in p ends with 'a', 'b', ..., 'z' and those substrings are all in s. Summary is the answer, according to the question.

Hope I made myself clear…

class Solution {
    public int findSubstringInWraproundString(String p) {
        int[] dp = new int[26]; // <结尾字母,以当前字母结尾的最长串长度>

        dp[p.charAt(0) - 'a'] = 1;
        int curLen = 1;
        for (int i = 1; i < p.length(); i++) {
            if (p.charAt(i - 1) == p.charAt(i) - 1 || p.charAt(i) == 'a' && p.charAt(i - 1) == 'z') {
                curLen++;
            } else {
                curLen = 1;
            }
            dp[p.charAt(i) - 'a'] = Math.max(dp[p.charAt(i) - 'a'], curLen);
        }

        int result = 0;
        for (int i = 0; i < 26; i++) {
            result += dp[i];
        }
        return result;
    }
}

在这里插入图片描述

标签:arr,String,int,467,++,curLen,字符串,dp,charAt
来源: https://blog.csdn.net/sinat_42483341/article/details/119928945

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有