leetcode:Minimum Window Substring题解(Java)
2016-07-08 20:49
429 查看
原题:
Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).
For example,
S =
T =
Minimum window is
Note:
If there is no such window in S that covers all characters in T, return the empty string
If there are multiple such windows, you are guaranteed that there will always be only one unique minimum window in S.
题意是要我们在给定的S字符串中找到包含T中所有字符的最小子串,要求复杂度是O(n)
暴力解法:从下标为0开始遍历S,设下标为i,设变量j从当前位置i遍历,一直到包含T的所有字符位置,比较i,j包含的长度,并保存最小的长度。提交结果:Time
Limit Exceeded,时间复杂度是O(n^2),不合题意
代码如下:
public String minWindow(String s, String t) {
if(s.length()<t.length() || t=="") return "";
String tempStr=null;
String str=s;
List<Character> charls = new ArrayList<Character>();
for (int i = 0; i < s.length()-t.length()+1; i++) {
charls.clear();
for (int k = 0; k < t.length(); k++) {
charls.add(t.charAt(k));
}
int j;
for ( j = i; j < s.length(); j++) {
if(charls.contains(s.charAt(j))){
charls.remove(charls.indexOf(s.charAt(j)));
}
if(charls.size()==0 || j==s.length()-1) break;
}
if(charls.size()==0){
tempStr=s.substring(i, j+1);
str=str.length()<tempStr.length()?str:tempStr;
}
if(i==0 && j==s.length()-1 && charls.size()>0) return "";
}
return str;
}
符合题意的解法:时间复杂度为O(n)
public String minWindow(String s, String t) {
if(t.length()>s.length())
return "";
String result = "";
HashMap<Character, Integer> target = new HashMap<Character, Integer>();
for(int i=0; i<t.length(); i++){
char c = t.charAt(i);
if(target.containsKey(c)){
target.put(c,target.get(c)+1);
}else{
target.put(c,1);
}
}
HashMap<Character, Integer> map = new HashMap<Character, Integer>();
int left = 0;
int minLen = s.length()+1;
int count = 0;
for(int i=0; i<s.length(); i++){
char c = s.charAt(i);
if(target.containsKey(c)){
if(map.containsKey(c)){
if(map.get(c)<target.get(c)){
count++;
}
map.put(c,map.get(c)+1);
}else{
map.put(c,1);
count++;
}
}
if(count == t.length()){
char sc = s.charAt(left);
while (!map.containsKey(sc) || map.get(sc) > target.get(sc)) {
if (map.containsKey(sc) && map.get(sc) > target.get(sc))
map.put(sc, map.get(sc) - 1);
left++;
sc = s.charAt(left);
}
if (i - left + 1 < minLen) {
result = s.substring(left, i + 1);
minLen = i - left + 1;
}
}
}
return result;
}
Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).
For example,
S =
"ADOBECODEBANC"
T =
"ABC"
Minimum window is
"BANC".
Note:
If there is no such window in S that covers all characters in T, return the empty string
"".
If there are multiple such windows, you are guaranteed that there will always be only one unique minimum window in S.
题意是要我们在给定的S字符串中找到包含T中所有字符的最小子串,要求复杂度是O(n)
暴力解法:从下标为0开始遍历S,设下标为i,设变量j从当前位置i遍历,一直到包含T的所有字符位置,比较i,j包含的长度,并保存最小的长度。提交结果:Time
Limit Exceeded,时间复杂度是O(n^2),不合题意
代码如下:
public String minWindow(String s, String t) {
if(s.length()<t.length() || t=="") return "";
String tempStr=null;
String str=s;
List<Character> charls = new ArrayList<Character>();
for (int i = 0; i < s.length()-t.length()+1; i++) {
charls.clear();
for (int k = 0; k < t.length(); k++) {
charls.add(t.charAt(k));
}
int j;
for ( j = i; j < s.length(); j++) {
if(charls.contains(s.charAt(j))){
charls.remove(charls.indexOf(s.charAt(j)));
}
if(charls.size()==0 || j==s.length()-1) break;
}
if(charls.size()==0){
tempStr=s.substring(i, j+1);
str=str.length()<tempStr.length()?str:tempStr;
}
if(i==0 && j==s.length()-1 && charls.size()>0) return "";
}
return str;
}
符合题意的解法:时间复杂度为O(n)
public String minWindow(String s, String t) {
if(t.length()>s.length())
return "";
String result = "";
HashMap<Character, Integer> target = new HashMap<Character, Integer>();
for(int i=0; i<t.length(); i++){
char c = t.charAt(i);
if(target.containsKey(c)){
target.put(c,target.get(c)+1);
}else{
target.put(c,1);
}
}
HashMap<Character, Integer> map = new HashMap<Character, Integer>();
int left = 0;
int minLen = s.length()+1;
int count = 0;
for(int i=0; i<s.length(); i++){
char c = s.charAt(i);
if(target.containsKey(c)){
if(map.containsKey(c)){
if(map.get(c)<target.get(c)){
count++;
}
map.put(c,map.get(c)+1);
}else{
map.put(c,1);
count++;
}
}
if(count == t.length()){
char sc = s.charAt(left);
while (!map.containsKey(sc) || map.get(sc) > target.get(sc)) {
if (map.containsKey(sc) && map.get(sc) > target.get(sc))
map.put(sc, map.get(sc) - 1);
left++;
sc = s.charAt(left);
}
if (i - left + 1 < minLen) {
result = s.substring(left, i + 1);
minLen = i - left + 1;
}
}
}
return result;
}
相关文章推荐
- Java核心技术-读书笔记 概览
- Eclipse配置Hadoop环境并运行WordCount样例
- 如何装饰Eclipse
- java基础教程10:事件处理机制
- Java实现文件远程上传和下载
- Java基础之多线程断点下载
- Java反射学习一
- Java反射学习二
- Java反射学习三
- Java反射学习四
- Spring-3:bean的属性配置细节
- java中volatile关键字的含义
- 记录:git删除误提交的Eclipse项目文件与配置文件
- 让eclipse启动时拥有jre
- ubuntu15.10 给解压版的eclipse安装桌面快捷方式
- 利用javaMail发送邮件
- java编程思想读书笔记
- 搭建Spring mvc环境
- 关于eclipse中配置svn插件,亲测有效。
- target,currentTarget和this三者的区别