欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

给定一个字符串,求出不含重复字符的最长子串长度

程序员文章站 2022-05-12 22:17:54
...

给定一个字符串,求出不含重复字符的最长子串长度

举例

输入:abcabca
输出:3
输入:bbbb
输出:1
输入:pwwkew
输出:3

我的想法

给定一个字符串,求出不含重复字符的最长子串长度

package s;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
public class sss {
 public static void main(String[] args) throws IOException {
  BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
   String str = br.readLine();
   int max=maxCountofString(str);
   System.out.println(max);
  
 }
 private static int maxCountofString(String str) {
  List<Integer> list=new ArrayList<>();
  HashMap<Integer,Integer> map1 = new HashMap<>();
  List<Integer> arrayList = new ArrayList<>();
  List<Integer> maxCount=new ArrayList<>();
  String b=str;
  int countOfTeam=0;
  while(b.length()>0) {
   arrayList.add(countOfTeam);
   list = getIndex(str,String.valueOf(b.charAt(0)));
   for(int j = 0;j<list.size();j++) {
    map1.put(list.get(j),countOfTeam);
   }
   b=b.replace(String.valueOf(b.charAt(0)),"");
   countOfTeam=countOfTeam+1;
  }
  for(int i=0;i<str.length();i++) {
   int m=i;
   int teamNumber=map1.get(i);
   int count=0;
   List<Integer> arrayList2 = new ArrayList<>();
   for(int j = 0;j<arrayList.size();j++) {
       arrayList2.add(arrayList.get(j));
   }
   while( arrayList2.indexOf(teamNumber)!=-1) {
    int indexOfteamNumber=arrayList2.indexOf(teamNumber);
    arrayList2.remove(indexOfteamNumber);
     count=count+1;
     m=m+1;
     if(m<str.length()) {
     teamNumber = map1.get(m);
   }
     }
   maxCount.add(count);
  }
 
  return(Collections.max(maxCount));
 }

 public static List<Integer> getIndex(String strings, String str){
     List<Integer> list=new ArrayList<>();
     int flag=0;
     while (strings.indexOf(str)!=-1){
         String aa= strings.substring(0,strings.indexOf(str)+str.length());
         flag=flag+aa.length();
         list.add(flag-str.length());
         strings=strings.substring(strings.indexOf(str)+str.length());`

反正能跑过去 hhhh

相关标签: 闲暇时光