Let be a string of lower case English letters. If there can be found all subsequences of length in , then can be divided into segments, each contains all the 26 letters, which implies length of is at least .
This observation leads us to a solution. Let be the maximum number of the aforementioned segments that the suffix of that starts at index can be divided into. The DP can be done in time. The shortest string that is not a subsequence of has a length of ( is 0-indexed).
Let be the position of the first occurrence of letter to the right of position (including position ). We can compute the array in time.
Using the and arrays, we can construct the answer as follows:
Start with an empty string . Iterate the positions of the answer string from left to right. For each position , iterate over the letters from 'a' to 'z'. For each letter , check whether it is possible to get an answer if we append to . Let be position of the last letter of the first occurrence of in as a subsequence, it is ok to append letter to if the suffix does not contain all subsequences of length i.e. . This check can be done efficiently, see the following code for detail.
code
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系统下SQL Server数据库镜像配置全流程详解
· 现代计算机视觉入门之:什么是视频
· 你所不知道的 C/C++ 宏知识
· 聊一聊 操作系统蓝屏 c0000102 的故障分析
· SQL Server 内存占用高分析
· 盘点!HelloGitHub 年度热门开源项目
· DeepSeek V3 两周使用总结
· 02现代计算机视觉入门之:什么是视频
· C#使用yield关键字提升迭代性能与效率
· 回顾我的软件开发经历(1)
2016-11-22 Palindrome Index
2016-11-22 Two-Pointer 之 Run Length Coding (RLC)
2016-11-22 求1...n中因子最多的数
2016-11-22 hackerrank Similar Pair
2016-11-22 区间第K大(一)
2016-11-22 POJ 2942 Knights of the Round Table
2016-11-22 Problems about trees