题解 | #牛群名字覆盖#
牛群名字覆盖
https://www.nowcoder.com/practice/e6cef4702e7841f59cf47daf9bafb241
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param s string字符串 * @param t string字符串 * @return string字符串 */ public String minWindow (String s, String t) { // write code here int[] hash = new int[128]; for (int i = 0; i < t.length(); i++) { hash[t.charAt(i)]--; } int slow = 0 , fast = 0; boolean isFind = false; String res = s; while(fast < s.length()){ hash[s.charAt(fast)]++; while(check(hash)){ isFind = true; if(res.length() > fast - slow + 1){ res = s.substring(slow , fast + 1); } hash[s.charAt(slow)]--; slow++; } fast++; } return isFind ? res : ""; } boolean check(int[] hash){ for (int i : hash) { if(i < 0) return false; } return true; } }