【Java】获取两个字符串中最大相同子串
题目
获取两个字符串中最大相同子串
前提
两个字符串中只有一个最大相同子串
解决方案
public class StringDemo {
public static void main(String[] args) {
String str1 = "abcwerthelloyuiodefabcdef";
String str2 = "cvhellobnm";
String str3 = getMaxSameString(str1, str2);
System.out.println(str3);
}
public static String getMaxSameString(String str1,String str2){
if(str1 != null && str2 != null){
String maxStr = (str1.length() >= str2.length()) ? str1 : str2;
String minStr = (str1.length() < str2.length()) ? str1 : str2;
int length = minStr.length();
//第一层循环控制最长字串的长度
for(int i = 0;i < length;i++){
//除了第一次循环是minStr的长度,其他循环的长度都有多肿可能,则需要遍历
//第二层循环从minStr下标为0开始,然后选取相应长度的子串
//每循环一次,子串的头部和尾部下标都加1以实现遍历当前长度的每一种情况
for(int head = 0,end = length-i;end <= length;head++,end++){
String subStr = minStr.substring(head,end);//substring左闭右开,所以end要取等号
if(maxStr.contains(subStr)){
return subStr;
}
}
}
}
return null;
}
}
结果

【Java】获取两个字符串中最大相同子串的更多相关文章
- 核心API的使用(获取两个字符串的最大相同子串)
/** * 获取两个字符串的最大相同子串. 例:abegad acegab */public class TheSameString { public static void main(String[ ...
- java基础知识回顾之---java String final类普通方法的应用之“两个字符串中最大相同的子串”
/* * 3,两个字符串中最大相同的子串. * "qwerabcdtyuiop" * "xcabcdvbn" * * 思路: * 1,既然取得是最大子串,先看 ...
- 【Java例题】5.5 两个字符串中最长公共子串
5. 查找两个字符串中含有的最长字符数的公共子串. package chapter5; import java.util.Scanner; public class demo5 { public st ...
- java 算法之 两个字符串中最大相同的子串
public class String_intern { public static void main(String[] args) { String old="aaaaabc1" ...
- java中获取两个时间中的每一天
引入下面方法即可: /** * 获取两个时间中的每一天 * @param bigtimeStr 开始时间 yyyy-MM-dd * @param endTimeStr 结束时间 yyyy-MM-dd ...
- java实现字符串匹配问题之求两个字符串的最大公共子串
转载请注明出处:http://blog.csdn.net/xiaojimanman/article/details/38924981 近期在项目工作中有一个关于文本对照的需求,经过这段时间的学习,总结 ...
- Java计算两个字符串日期之间的天数差
Java计算两个字符串日期之间的天数差 调用方法: public static void main(String[] args) throws ParseException { String a = ...
- Java中用正则表达式截取字符串中
Java中用正则表达式截取字符串中第一个出现的英文左括号之前的字符串.比如:北京市(海淀区)(朝阳区)(西城区),截取结果为:北京市.正则表达式为() A ".*?(?=\\()" ...
- [原]Java面试题-将字符串中数字提取出来排序后输出
[Title][原]Java面试题-将字符串中数字提取出来排序后输出 [Date]2013-09-15 [Abstract]很简单的面试题,要求现场在纸上写出来. [Keywords]面试.Java. ...
随机推荐
- Wireshark(三):应用Wireshark IO图形工具分析数据流
原文出处: EMC中文支持论坛 基本IO Graphs: IO graphs是一个非常好用的工具.基本的Wireshark IO graph会显示抓包文件中的整体流量情况,通常是以每秒为单位(报文数或 ...
- 微信小程序项目使用npm安装vant-weapp的正确步骤,简单易懂!!
微信小程序项目使用npm安装vant-weapp的正确步骤 1.在当前小程序项目目录npm init -y 构建npm项目 2.运行命令 npm install vant-weapp -S --pro ...
- CF1080B Margarite and the best present 题解
Content 有 \(t\) 次询问,每次询问给定两个整数 \(l,r\),求 \(\sum\limits_{i=l}^r (-1)^i\times i\). 数据范围:\(1\leqslant t ...
- 后端调用WebApi
using System;using System.Collections.Generic;using System.Linq;using System.Net.Http;using System.W ...
- HTML5 head标签meta标签、title的功能
<!DOCTYPE html> <!-- 解释器--> <html lang="en"> <head> <!--meta标签中 ...
- 奇怪的现象,打印出来可以见到数据,在右边看不到数据,放到list里在页面上也显示不了数据
- Visiual Studio之c++项目瘦身(删除中间项)
欢迎指正 本文主要涉及 Visiual Studio(简称VS) 创建的c++项目 和 windows下批处理相关点. 1.中间项 A.VS创建的c++项目,生成后,会有许多中间项,包括项目生成的中间 ...
- 【LeetCode】Balanced Binary Tree 算法优化 解题报告
Balanced Binary Tree Better Solution [LeetCode] https://leetcode.com/submissions/detail/40087813/ To ...
- 【LeetCode】820. 单词的压缩编码 Short Encoding of Words(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode-cn.com/problems/short- ...
- TensorFlow.NET机器学习入门【5】采用神经网络实现手写数字识别(MNIST)
从这篇文章开始,终于要干点正儿八经的工作了,前面都是准备工作.这次我们要解决机器学习的经典问题,MNIST手写数字识别. 首先介绍一下数据集.请首先解压:TF_Net\Asset\mnist_png. ...