求一个字符串的最长无重复字母的连续子串
2014-08-24 22:10
281 查看
如字符串“abacdabefac”, 则其最长无重复字母的连续子串是:“cdabef”
java实现如下:
package com.test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class FindSubString {
public static String findSubStr(String str) {
Map<Character, Integer> indexMap = new HashMap<Character, Integer>();
if (null == str || str.isEmpty()) {
return str;
}
char[] tmpList = new char[str.length()];
String result = null;
int tmpLength = 0;
for (int i = 0; i < str.length();) {
char c = str.charAt(i);
if (indexMap.get(c) == null) {
indexMap.put(c, i);
tmpList[tmpLength++] = c;
i++;
} else {
if (null == result || result.length() < tmpLength) {
result = new String(tmpList, 0, tmpLength);
}
tmpLength = 0;
i = indexMap.get(c) + 1;
indexMap.clear();
}
}
if (null == result || result.length() < tmpLength) {
result = new String(tmpList, 0, tmpLength);
}
return result;
}
public static String findSubStrImprove1(String str) {
Map<Character, Integer> indexMap = new HashMap<Character, Integer>();
if (null == str || str.isEmpty()) {
return str;
}
int preStart = 0;
int preEnd = 0;
int start = 0;
int end = 0;
for (int i = 0; i < str.length();) {
char c = str.charAt(i);
if (indexMap.get(c) == null) {
indexMap.put(c, i);
} else {
if ((preEnd - preStart) < (end - start)) {
preStart = start;
preEnd = end;
}
start = indexMap.get(c) + 1;
for(int j = 0; j < start; j++){
indexMap.remove(str.charAt(j));
}
/*
System.out.print("counting " + i + " th char:" + c + ", appeared before, current longest: "
+ str.substring(preStart, preEnd) + ",start = "+ preStart+",end = " +preEnd + "\t");
System.out.println("now start = " + start);*/
indexMap.put(c, i);
}
i++;
end++;
}
if ((preEnd - preStart) >= (end - start)) {
start = preStart;
end = preEnd;
}
return str.substring(start, end);
}
public static void main(String[] args) {
String str = "abacdabefac";//output:cdabef
System.out.println(findSubStr(str));
System.out.println(findSubStrImprove1(str));
str = "afhjufamtbdehtskoitdc";//output:hjufamtbde
System.out.println(findSubStr(str));
System.out.println(findSubStrImprove1(str));
str = "ababcdefgcdabmn";//output:efgcdabmn
System.out.println(findSubStr(str));
System.out.println(findSubStrImprove1(str));
}
}
java实现如下:
package com.test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class FindSubString {
public static String findSubStr(String str) {
Map<Character, Integer> indexMap = new HashMap<Character, Integer>();
if (null == str || str.isEmpty()) {
return str;
}
char[] tmpList = new char[str.length()];
String result = null;
int tmpLength = 0;
for (int i = 0; i < str.length();) {
char c = str.charAt(i);
if (indexMap.get(c) == null) {
indexMap.put(c, i);
tmpList[tmpLength++] = c;
i++;
} else {
if (null == result || result.length() < tmpLength) {
result = new String(tmpList, 0, tmpLength);
}
tmpLength = 0;
i = indexMap.get(c) + 1;
indexMap.clear();
}
}
if (null == result || result.length() < tmpLength) {
result = new String(tmpList, 0, tmpLength);
}
return result;
}
public static String findSubStrImprove1(String str) {
Map<Character, Integer> indexMap = new HashMap<Character, Integer>();
if (null == str || str.isEmpty()) {
return str;
}
int preStart = 0;
int preEnd = 0;
int start = 0;
int end = 0;
for (int i = 0; i < str.length();) {
char c = str.charAt(i);
if (indexMap.get(c) == null) {
indexMap.put(c, i);
} else {
if ((preEnd - preStart) < (end - start)) {
preStart = start;
preEnd = end;
}
start = indexMap.get(c) + 1;
for(int j = 0; j < start; j++){
indexMap.remove(str.charAt(j));
}
/*
System.out.print("counting " + i + " th char:" + c + ", appeared before, current longest: "
+ str.substring(preStart, preEnd) + ",start = "+ preStart+",end = " +preEnd + "\t");
System.out.println("now start = " + start);*/
indexMap.put(c, i);
}
i++;
end++;
}
if ((preEnd - preStart) >= (end - start)) {
start = preStart;
end = preEnd;
}
return str.substring(start, end);
}
public static void main(String[] args) {
String str = "abacdabefac";//output:cdabef
System.out.println(findSubStr(str));
System.out.println(findSubStrImprove1(str));
str = "afhjufamtbdehtskoitdc";//output:hjufamtbde
System.out.println(findSubStr(str));
System.out.println(findSubStrImprove1(str));
str = "ababcdefgcdabmn";//output:efgcdabmn
System.out.println(findSubStr(str));
System.out.println(findSubStrImprove1(str));
}
}
相关文章推荐
- 求一个字符串中最长的非重复连续子串
- 用三重循环求一个字符串的最大回文串(连续重复出现的最长子串)
- 【字符串问题】求一个字符串中重复出现的最长的子串
- 【寻找一个字符串中最长的重复子串】
- 在一个字符串中寻找到最长重复子串(1)
- 字符串中连续最长重复子串
- 每日面试题:给定一个字符串,求出其最长的重复子串
- 给定一个字符串,输出最长的重复子串
- 给定一个字符串,输出最长的重复子串
- KMP算法应用------求解一个字符串的最长重复子串
- 【每天学点算法题10.17】寻找一个字符串中的最长重复子串
- 求一个字符串中的最长连续子串
- 在字符串中查找连续重复的最长子串的C++实现
- 找出一个字符串中最长连续相同子串
- 求一个字符串当中的最长重复子串
- 求一个字符串中最长连续子串
- 求一个字符串中字符不重复的连续的子串
- 求一个字符串中的最大连续重复子串
- 字符串操作问题:查找给定字符串中,连续重复且长度最长的第一个子串
- 给一个字符串,例如”ababc",要求返回"ab"。因为"ab”连续重复出现且最长。用C/C++语言写一个函数完成该算法,给出复杂度