题目链接 https://leetcode-cn.com/problems/distinct-subsequences-ii/ 题意: 给定一个字符串,判断里面不相同的子串的总个数 思路: 非常巧妙的一个题: 以"abc"为例:不同的子序列有:{a,b,c,ab,ac,bc,abc} 朴素解法:O(2^N) 优化解法:动态规划+字符串hash O(26*N) 设dp[i]表示以'a'+i结尾的字符所含有的不同的子序列的个数 那么根据s[i-1]的情况,最多也就26种可能的情况,即dp[…
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归求解 日期 题目地址:https://leetcode.com/problems/word-break-ii/ 题目描述 Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add spaces in s to con…
Design a class which receives a list of words in the constructor, and implements a method that takes two words word1 and word2 and return the shortest distance between these two words in the list. Your method will be called repeatedly many times with…
This is a follow up of Shortest Word Distance. The only difference is now you are given the list of words and your method will be called repeatedly many times with different parameters. How would you optimize it? Design a class which receives a list…
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from letters of sequentially adjacent cell, where "adjacent&quo…
problem: Given an input string, reverse the string word by word. For example: Given s = "the sky is blue", return "blue is sky the". 问题分析:如何准确的找到每一个需要清除的空格的位置pos,以及每个word对应的pos范围? 解决方法:需要反转一个字符串,使用int string.find_last_not_of(char c, in…
Given a list of words and two words word1 and word2, return the shortest distance between these two words in the list. For example,Assume that words = ["practice", "makes", "perfect", "coding", "makes"]. G…
This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as word2. Given a list of words and two words word1 and word2, return the shortest distance between these two words in the list. word1 and word2 may be…
leetcode面试准备: Word Pattern 1 题目 Given a pattern and a string str, find if str follows the same pattern. Examples: pattern = "abba", str = "dog cat cat dog" should return true. pattern = "abba", str = "dog cat cat fish&qu…
Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be us…