最长不连续子串

import java.util.HashMap;  
import java.util.Map;  
public class zuichang {  
    public static void main(String[] args) {  
        longestNodupSubstring("1201354351");  
    }  
    /**cursor里面存放字符的在字符串中的位置 
     * lengAt[i]存放以字符string.charAt(i)结尾的最长子字符串的长度 
     * @param string 
     */  
    public static void longestNodupSubstring(String string)  
    {  
        int len = string.length();//字符串长度  
        if(len>0){  
            Map<Character,Integer> cursor = new HashMap<Character,Integer>();  
            cursor.put(string.charAt(0), 0); //将字符串中首字母添加到map中去 
            int [] lengthAt = new int[string.length()];// 声明一个数组存放最长字符串的长度
            lengthAt[0] =1;  //此时map字符中对应的长度是
            int max =0;  
            for(int i = 1 ;i<len;i++){  
                char c = string.charAt(i);  
                if(cursor.containsKey(c)){  
                    lengthAt[i] = Math.min(lengthAt[i-1]+1, i-cursor.get(c));     
                }else {  
                    lengthAt[i] = lengthAt[i-1]+1;  
                }  
                max = Math.max(max, lengthAt[i]);  
                cursor.put(c, i);  
            }  
            for(int i=0;i<len;i++){  
                if(max == lengthAt[i]){  
                    System.out.println(string.substring(i-max+1, i+1));  
                }  
            }  
        }  
    }  
  
}  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值