最大字串

字符串的最大子串么

代码比较简单。。直接上吧。大学时期的程序

import java.util.LinkedList;
import java.util.List;

public class MaxString {

	public static void main(String[] args) {
		List<Character> str1 = new LinkedList<Character>();
		List<Character> str2 = new LinkedList<Character>();
		str1.add(new Character('a'));
		str1.add(new Character('b'));
		str1.add(new Character('a'));
		str1.add(new Character('c'));
		str1.add(new Character('h'));
		
		str2.add(new Character('i'));
		str2.add(new Character('a'));
		str2.add(new Character('b'));
		str2.add(new Character('a'));
		str2.add(new Character('c'));
		str2.add(new Character('h'));
		str2.add(new Character('q'));
		String sub = maxSub(str1,str2);
		System.out.println(sub);
	}

	private static String maxSub(List<Character> str1, List<Character> str2) {
		if(str1.size()<=0||str2.size()<=0){
			return null;
		}
		int k=0;
		while(str1.get(k).equals(str2.get(k))){
			k++;
			if (k>=str1.size()||k>=str2.size()){
				break;
			}
		}
		if (k == 0) {
			//the first one is not equal....
			str2.remove(k);
			//just go to the next iteration
			return maxSub(str1,str2);
		} else {
			StringBuilder sb = new StringBuilder();
			for(int i=0;i<k;i++) {
				sb.append(str1.get(i));
			}
			for(int i=0;i<k;i++) {
				str1.remove(0);str2.remove(0);
			}
			String subString = maxSub(str1, str2);
			if (subString != null&&(subString.length()>sb.length())) {
				return subString;
			} else {
				return sb.toString();
			}
		}
		
	}


}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值