Q467 环绕字符串中唯一的子字符串
把字符串 s
看作是“abcdefghijklmnopqrstuvwxyz”的无限环绕字符串,所以 s
看起来是这样的:"...zabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd....".
现在我们有了另一个字符串 p
。你需要的是找出 s
中有多少个唯一的 p
的非空子串,尤其是当你的输入是字符串 p
,你需要输出字符串 s
中 p
的不同的非空子串的数目。
注意: p
仅由小写的英文字母组成,p 的大小可能超过 10000。
示例 1:
输入: "a"
输出: 1
解释: 字符串 S 中只有一个"a"子字符。
示例 2:
输入: "cac"
输出: 2
解释: 字符串 S 中的字符串“cac”只有两个子串“a”、“c”。.
示例 3:
输入: "zab"
输出: 6
解释: 在字符串 S 中有六个子串“z”、“a”、“b”、“za”、“ab”、“zab”。.
class Solution {
public int findSubstringInWraproundString(String p) {
if (p == null || p.length() == 0)
return 0;
int[] nums = new int[26];
char[] chs = p.toCharArray();
nums[chs[0] - 'a'] = 1;
int t = 2;
for (int i = 1; i < chs.length; i++) {
if (chs[i] != chs[i - 1] + 1 && chs[i] != chs[i - 1] - 25) {
t = 1;
}
int k = nums[chs[i] - 'a'];
nums[chs[i] - 'a'] = k > t ? k : t;
t++;
}
int result = 0;
for (int i : nums)
result += i;
return result;
}
}
Q467 环绕字符串中唯一的子字符串的更多相关文章
- Java实现 LeetCode 467 环绕字符串中唯一的子字符串
467. 环绕字符串中唯一的子字符串 把字符串 s 看作是"abcdefghijklmnopqrstuvwxyz"的无限环绕字符串,所以 s 看起来是这样的:"-zabc ...
- [Swift]LeetCode467. 环绕字符串中唯一的子字符串 | Unique Substrings in Wraparound String
Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...
- leetcode 467. 环绕字符串中唯一的子字符串
题目描述: 把字符串 s 看作是“abcdefghijklmnopqrstuvwxyz”的无限环绕字符串,所以 s 看起来是这样的:"...zabcdefghijklmnopqrstuvwx ...
- python判断字符串中是否包含子字符串
python判断字符串中是否包含子字符串 s = '1234问沃尔沃434' if s.find('沃尔沃') != -1: print('存在') else: print('不存在' ...
- [LeetCode] Unique Substrings in Wraparound String 封装字符串中的独特子字符串
Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...
- 467 Unique Substrings in Wraparound String 封装字符串中的独特子字符串
详见:https://leetcode.com/problems/unique-substrings-in-wraparound-string/description/ C++: class Solu ...
- js 判断字符串中是否包含某个字符串的方法实例
String对象的方法 方法一: indexOf() (推荐) var str = "123"; console.log(str.indexOf("3") ...
- js 判断字符串中是否包含某个字符串
String对象的方法 方法一: indexOf() (推荐) var str = "123"; console.log(str.indexOf("3") ...
- js 判断字符串中是否包含某个字符串(转载)
from : https://www.cnblogs.com/ooo0/p/7741651.html String对象的方法 方法一: indexOf() (推荐) var str = " ...
随机推荐
- [Cookie] Read Cookie and Pass in headers
在同一个Suite里 import com.eviware.soapui.support.types.StringToStringMap def headers = testRunner.testCa ...
- [SoapUI] 比较两个不同环境下XML格式的Response, 结果不同时设置Test Step的执行状态为失败
import org.custommonkey.xmlunit.* def responseTP=context.expand( '${Intraday Table_TP#Response}' ) d ...
- Centos配置多个tomcat服务器,并用nginx实现负载均衡
centos配置tomcat请参见上一篇博文 :http://www.cnblogs.com/nanyangzp/p/4897655.html 一:多tomcat利用不同端口开启服务器 多个tomca ...
- eclipse配置tomcat 8.0.24服务器
开发j2ee,肯定需要web容器来运行web项目,目前web容器有tomcat,jetty,Jboss,weblogic等服务器,最为常用的是tomcat服务器,其与eclipse的配置如下: tom ...
- OSI结构和TCP/IP模型
TCP/IP层次模型共分为五层:应用层HTTP.传输层TCP.网络层IP.数据链路层Data-link.物理层physical. 应用层—应用层是所有用户所面向的应用程序的统称.ICP/IP协议族在这 ...
- Verilog MIPS32 CPU(九)-- 顶层文件
`timescale 1ns / 1ps /////////////////////////////////////////////////////////////////////////////// ...
- xshell无法连接Ubuntu的解决办法
使用workstations14安装完Ubunu后,网络连接方式为NAT模式(N):用于共享主机的IP地址 此时想用xshell连接此虚拟机但是提示连接失败,但是宿主机和虚拟机互相都能ping通,且虚 ...
- RobotFramework做接口自动化(内部接口需要登录token)
背景: 项目中需要做接口自动化测试,使用robot,有一个收货地址列表的接口直接传参数访问时会返回:{"msg":"缺少参数","code" ...
- 单例模式(Singleton)小记
概念 引用维基百科对单例的说明: 单例模式,也叫单子模式,是一种常用的软件设计模式.在应用这个模式时,单例对象的类必须保证只有一个实例存在. 继续引用维基百科的实现思路: 实现单例模式的思路是:一个类 ...
- [NetCore学习记录]第一章.使用netcore撸个简单的增删改查
1.引言 2.解决方案各部分介绍图 3.添加数据模型 4.添加数据库上下文 5.修改配置文件 6.使用依赖关系注入容器注册数据库上下文 7.添加基架工具并执行初始迁移 1.引言 NetCore出来有一 ...