寻找最小子字符串

小美和小团在玩一个游戏,小美任意给出一个大字符串str1以及一个独立的小字符串str2,小团需要从这个大字符串str1里找到包含独立小字符串str2中所有字符的最小子字符串str3;
例如,小美给出一个大字符串"meituan2019"和一个子字符串"i2t",那么小团给出的答案就应该是"ituan2";

需要注意:
1、str1中有可能没有完整包含str2所有字符的情况,此时返回"",即为空字符串;
2、str1不会为空,但str2有可能为空,此时返回整个str1;
3、str2可能存在重复的字符,此时str3需要包含相等数量该字符;

 

输入例子1:

"meituan2019","i2t"

 

输出例子1:

"ituan2"
import java.util.*;
public class Solution {
    /**
     *
     * @param str1 string字符串
     * @param str2 string字符串
     * @return string字符串
     */
    public String getMinString (String str1, String str2) {
        if(str2.length() == 0) return str1;
        HashMap<Character,Integer> map = new HashMap<Character,Integer>();
        for(int i = 0; i<str2.length(); i++){
            char c = str2.charAt(i);
            if(!map.containsKey(c)){
                map.put(c,1);
            }else{
                int tmp = map.get(c);
                map.put(c,++tmp);
            }
        }
        boolean flag = true; //是否包含str2中字符的标志,若始终未包含,则为true
        int start = -1,end = -1;
        for(int i = 0; i < str1.length(); i++){
            char c = str1.charAt(i);
            if(map.containsKey(c)){
                flag = false;
                if(start == -1){
                    start = i;
                }
                end = i;
                int cnt = map.get(c);
                if(cnt - 1 >= 0){
                    map.put(c,--cnt);
                }
            }
        }
        if(flag){
            return "";
        }
        Iterator iter = map.entrySet().iterator();
        boolean flag2 = true; //是否全 默认为每个字符都在str1中匹配到了相应个数
        while(iter.hasNext()){
            Map.Entry entry = (Map.Entry)iter.next();
            int value = (int)entry.getValue();
            if(value != 0) {
                flag2 = false; //不全 false
                break;
            }
        }
        if(!flag2){
            return "";
        }else{
            return str1.substring(start,end+1); //substring左开右闭,所有右索引+1
        }
    }
}

 

你可以使用滑动窗口算法来解决这个问题。下面是一个用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、付费专栏及课程。

余额充值