记录 ----- 给定两个字符串,求最小子串

给定两个字符串,求最小子串

给定一个字符串 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 "没有找到对应的最小子串";
        }
    }
}

你可以使用滑动窗口算法来解决这个问题。下面是一个用C语言实现的示例代码: ```c #include <stdio.h> #include <string.h> #define MAX_CHARS 256 // 判断两个字符数组是否相等 int isSame(int countP[], int countQ[]) { for (int i = 0; i < MAX_CHARS; i++) { if (countP[i] != countQ[i]) { return 0; } } return 1; } void findMinSubstring(char A[], char B[]) { int lenA = strlen(A); int lenB = strlen(B); if (lenA < lenB) { printf("No such window exists"); return; } // 统计B每个字符出现的次数 int countB[MAX_CHARS] = {0}; for (int i = 0; i < lenB; i++) { countB[B[i]]++; } // 统计A每个字符出现的次数 int countA[MAX_CHARS] = {0}; int start = 0, startIndex = -1, minLength = INT_MAX; int count = 0; for (int j = 0; j < lenA; j++) { // 统计A每个字符出现的次数 countA[A[j]]++; // 找到B的所有字符 if (countB[A[j]] != 0 && countA[A[j]] <= countB[A[j]]) { count++; } // 如果找到了B的所有字符 if (count == lenB) { // 移除多余的字符 while (countA[A[start]] > countB[A[start]] || countB[A[start]] == 0) { if (countA[A[start]] > countB[A[start]]) { countA[A[start]]--; } start++; } // 更新最小长度和起始索引 int windowLen = j - start + 1; if (windowLen < minLength) { minLength = windowLen; startIndex = start; } } } // 没有找到符合条件的子串 if (startIndex == -1) { printf("No such window exists"); return; } // 打印最小子串 for (int k = startIndex; k < startIndex + minLength; k++) { printf("%c", A[k]); } } int main() { char A[] = "ADOBECODEBANC"; char B[] = "ABC"; findMinSubstring(A, B); return 0; } ``` 这个示例代码,我们通过滑动窗口算法来寻找在字符串A包含字符串B的最小子串。我们使用两个字符数组`countA`和`countB`来分别统计字符串A和B每个字符出现的次数。然后,我们使用两个指针`start`和`startIndex`来表示窗口的起始位置,以及一个变量`minLength`来记录当前找到最小子串的长度。我们逐步向右移动指针,更新窗口内的字符计数,并在窗口内找到了所有B字符时进行处理。如果找到了比当前最小子串长度更小的子串,我们更新`minLength`和`startIndex`。最后,我们打印出最小子串。 对于给定的示例输入,上述代码将输出 "BANC",这是字符串A包含字符串B的最小子串
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值