给定两个字符串,求最小子串
给定一个字符串 S 和一个字符串 T,请在 S 中找出包含 T 所有字母的最小子串。
示例:
输入: S = "ADOBECODEBANC", T = "ABC"
输出: "BANC"
说明:
* 如果 S 中不存这样的子串,则返回空字符串 `""` 。
package duzhicheng.mapdemo;
import org.springframework.util.StringUtils;
public class Demo2 {
private static int cun = 0;
private static int cun1 = 0;
public static void main(String[] args) {
String a = "ADOBANCDEBANC";
String b = "ABC";
String str = getStr(a, b);
System.out.println(str);
}
public static String getStr(String a , String b){
try {
cun ++;
if(StringUtils.isEmpty(a) || StringUtils.isEmpty(b)){
return "字符串为空";
}
if(a.length() < b.length()){
return "父字符串长度小于子字符串";
}
int length_b = b.length();
int start = cun-1;
if(start > (a.length() - b.length()-cun1)){
start = 0;
cun = 1;
cun1 ++;
}
int end = start+length_b + cun1;
String c = a.substring(start,end);
int count = 0;
String bb = b;
for(int i = 0 ; i < c.length(); i++){
String value = String.valueOf(c.charAt(i));
if(bb.contains(value)){
bb = bb.replace(value, "");
count++;
}
}
if(count == length_b){
return c;
}else {
String str = getStr(a, b);
return str;
}
} catch (Exception e) {
return "没有找到对应的最小子串";
}
}
}