当前位置:网站首页>判断字符串是否有子字符串重复出现
判断字符串是否有子字符串重复出现
2022-08-04 05:27:00 【第一片心意】
1. 功能说明
判断一个字符串中是否有重复的子字符串出现,比如字符串
我爱足球,我爱篮球,我爱排球。
“我爱”这个子字符串出现了三次。
2. 代码实现
public class RepetitionString {
/**
* 判断字符串中是否包含重复的子串<br>
* 如果有多个子字符串重复,则返回重复次数最多的字符串<br>
* 如果多个子字符串重复次数一样,则返回最长的子字符串
*
* @param value 带判断字符串
* @param minLength 子字符串最小判断长度
* @param repetitionCount 重复次数,当重复次数达到该值,才认为原字符串包含重复字串
* @return 是否包含子串,如果包含子串,也将子串返回
*/
public String core(String value, int minLength, int repetitionCount) {
Map<String, Integer> subCount = new HashMap<>(16);
Set<String> subStrings = new HashSet<>();
for (int start = 0; start < value.length(); start++) {
handle(subCount, value.substring(start), minLength);
}
//查看子字符串重复最大次数
int maxCount = 0;
for (Map.Entry<String, Integer> entry : subCount.entrySet()) {
if (entry.getValue() > maxCount) {
maxCount = entry.getValue();
subStrings.clear();
subStrings.add(entry.getKey());
} else if (entry.getValue() == maxCount) {
subStrings.add(entry.getKey());
}
}
//根据设定重复次数返回结果
if (maxCount >= repetitionCount) {
int maxLength = 0;
String maxLengthSubString = "";
for (String subString : subStrings) {
if (subString.length() > maxLength) {
maxLength = subString.length();
maxLengthSubString = subString;
}
}
return "true:" + maxLengthSubString + "->" + maxCount;
} else {
return "false";
}
}
/**
* 遍历子串长度,处理数据
*
* @param subCount 子串及其重复数量
* @param value 待处理字符串
* @param minLength 子字符最小判断长度
*/
private void handle(Map<String, Integer> subCount, String value, int minLength) {
for (int subLength = minLength; subLength <= value.length() / 2; subLength++) {
//从起始位置取子串长度的子字符串
String substring = value.substring(0, subLength);
if (subCount.containsKey(substring)) {
continue;
}
for (int subStart = 0; subStart < value.length() - subLength + 1; subStart++) {
//从起始位置开始取子串长度的字符串和上面待判断的子字符串进行对比
String temp = value.substring(subStart, subStart + subLength);
if (Objects.equals(substring, temp)) {
if (subCount.containsKey(substring)) {
subCount.put(substring, subCount.get(substring) + 1);
} else {
subCount.put(substring, 1);
}
}
}
}
}
public static void main(String[] args) {
RepetitionString r = new RepetitionString();
String value1 = "我爱足球,我爱篮球,我爱排球。";
String value2 = "我爱足球,我喜欢打篮球";
int minLength = 2;
int repetitionCount = 2;
System.out.println(value1 + "\n\t" + r.core(value1, minLength, repetitionCount));
System.out.println(value2 + "\n\t" + r.core(value2, minLength, repetitionCount));
}
}运行结果:

边栏推荐
- flink-sql所有表格式format
- [NSSRound#1 Basic]
- MediaCodec支持的类型
- The cost of automated testing is high and the effect is poor, so what is the significance of automated testing?
- 将两个DataTable合并——DataTable.Merge 方法
- 程序、进程、线程、协程的概念及区别
- Deploy LVS-DR cluster [experimental]
- warning C4251: “std::vector&lt;_Ty&gt;”需要有 dll 接口由 class“Test”的客户端使用错误
- 12. Paging plugin
- 对象存储-分布式文件系统-MinIO-1:概念
猜你喜欢
随机推荐
利用Jenkins实现Unity自动化构建
webrtc中视频采集实现分析(二) 视频帧的分发
JS实现上一个、下一个、置顶、置底操作
flink-sql所有表格式format
页面刷新没有执行watch?
【树 图 科 技 头 条】2022年6月27日 星期一 今年ETH2.0无望
NFT市场可二开开源系统
使用express-jwt第三方包报错TypeError: expressJWT is not a function
二月、三月校招面试复盘总结(一)
The string class introduction
ThinkPHP5.0.x 反序列化分析
多个gcc/glibc版本的共存及指定gcc版本的编译
二月、三月校招面试复盘总结(二)
TensorRT例程解读之语义分割demo
php实现telnet访问端口
Unity表格配置编辑工具
将自定义类型作为关联容器的key
智能合约安全——私有数据访问
自动化运维工具Ansible(4)变量
Handling List






![Embedded system driver primary [4] - under the basis of character device driver _ concurrency control](/img/96/5224d2de152eb738703cd201fb8407.png)


