给定一个字符串s,请你找出其中不含有重复字符的最长子串的长度。
package LeetCode; import java.util.HashSet; /** * @program:多线程和IO * @descripton:给定一个字符串s,请你找出其中不含有重复字符的最长子串的长度 * @author:ZhengCheng * @create:2021/9/19-21:02 **/ public class LongestSubstring { public static void main(String[] args) { String s = "abcdefgacdefgahijklmabcefghiajklmnopq"; int i = FindLongestSubstring(s); System.out.println(i); } public static int FindLongestSubstring(String s){ char[] str = s.toCharArray(); int i = 0; HashSet<Object> set = new HashSet<>(); int cnt = 0; int max = 0; while (i < str.length){ if (set.add(str[i])){ cnt++; }else { if (cnt > max){ max = cnt; } set.clear(); set.add(str[i]); cnt = 1; } i++; } return cnt>max?cnt:max; } }