从字符串S中找出不包含重复字符的最大连续子字符串长度
2009-10-03 19:52
225 查看
package algorithm;
import java.util.Scanner;
/**
* 从字符串S中找出不包含重复字符的最大连续子字符串长度,
* 如1:"abcdapp"最长合法子串为"bcdap"返回长度为5
* 2:"aaaaaaa"最长合法子串为"a",返回长度为1
* @author Administrator
*
*/
public class NonRepeatedSubString {
public static void main(String[]args){
Scanner scanner=new Scanner(System.in);
String str=scanner.nextLine();
findMaxNonReaptedString(str);
}
public static void findMaxNonReaptedString(String str)
{
int begin=0;
int end=0;
int maxLength=0;
int tempLength=0;
int k=0;
char c=0;
for(int i=0,j=0;j<str.length();j++)
{
String source=str.substring(i,j);
System.out.println(source);
c=str.charAt(j);
k=contains(source,c);
if(k>-1)
{
i=i+k+1;
tempLength=j-i+1;
}
else{
tempLength+=1;
if(tempLength>maxLength)
{
maxLength=tempLength;
begin=i;
end=j;
}
}
}
System.out.println("the max substring without repeated char is:"+str.substring(begin,end+1));
}
public static int contains(String str,char c)
{
String s=Character.toString(c);
return str.indexOf(s);
}
}
import java.util.Scanner;
/**
* 从字符串S中找出不包含重复字符的最大连续子字符串长度,
* 如1:"abcdapp"最长合法子串为"bcdap"返回长度为5
* 2:"aaaaaaa"最长合法子串为"a",返回长度为1
* @author Administrator
*
*/
public class NonRepeatedSubString {
public static void main(String[]args){
Scanner scanner=new Scanner(System.in);
String str=scanner.nextLine();
findMaxNonReaptedString(str);
}
public static void findMaxNonReaptedString(String str)
{
int begin=0;
int end=0;
int maxLength=0;
int tempLength=0;
int k=0;
char c=0;
for(int i=0,j=0;j<str.length();j++)
{
String source=str.substring(i,j);
System.out.println(source);
c=str.charAt(j);
k=contains(source,c);
if(k>-1)
{
i=i+k+1;
tempLength=j-i+1;
}
else{
tempLength+=1;
if(tempLength>maxLength)
{
maxLength=tempLength;
begin=i;
end=j;
}
}
}
System.out.println("the max substring without repeated char is:"+str.substring(begin,end+1));
}
public static int contains(String str,char c)
{
String s=Character.toString(c);
return str.indexOf(s);
}
}
相关文章推荐
- lc#3求字符串中不包含重复字符的最大长度子串
- 02_对N个长度最长可达到1000的数进行排序、对给定的一个字符串,找出有重复的字符,并给出其位置、输入一系列整数,将其中最大的数挑出,并将剩下的数进行排序
- 找出字符串中最大子串的长度(要求子串中的字符不重复)
- 字符串中不重复连续字符子串的长度最大值
- Longest Substring Without Repeating Characters计算字符串中不含有重复字符的连续字符串的最大长度
- 求字符串中不含重复字符的最长子串的长度
- 求一字符串最长不重复字符子串的长度【Java 版】
- 华为机试题:输入两个超长整型构成的字符串,其间使用一个空格分隔,每个字符串最大长度为100个字符。求第一个整数除以第二个整数以后的余数。。
- (10.31)字符串由字母和数字组成,找出其中最大数字(长度0<n<=1000)
- 【谷歌面试题】找出字符串中只包含两种字符的最长子串
- java中获取包含中文字符的字符串长度
- 找出字符串最长不重复子串,输出长度
- 找出字符串中第一个不重复出现的字符(仅小写字母)
- 查找字符串中字符不重复的最大子串(C/C++)
- 求两个字符串去掉重复字母后所包含字符
- 输入一行字符串,找出其中重复出现且最长的字符串,输出它及其首字符的位置
- 每组数据输入一个字符串,字符串最大长度为100,且只包含字母,不可能为空串,区分大小写。
- 05年华中科大机试第一题(对给定的一个字符串,找出有重复的字符,并给出其位置)
- 一个字符串中可能包含a~z中的多个字符,如有重复,求出现次数最多的那个字母及次数,如有多个重复最多的则都输出。
- 有一个长度不大于40的字符串,已知其中包含两个字符‘A',求处于这两个字符之间的字符的个数,并把这些字符依次打印出来