Find longest substring without repeating characters

前端 未结 30 2567
轻奢々
轻奢々 2020-12-12 18:07

Given a string S of length N find longest substring without repeating characters.

Example:

Input:

30条回答
  •  被撕碎了的回忆
    2020-12-12 18:44

    EDITED:

    following is an implementation of the concesus. It occured to me after my original publication. so as not to delete original, it is presented following:

    public static String longestUniqueString(String S) {
        int start = 0, end = 0, length = 0;
        boolean bits[] = new boolean[256];
        int x = 0, y = 0;
        for (; x < S.length() && y < S.length() && length < S.length() - x; x++) {
            bits[S.charAt(x)] = true;
            for (y++; y < S.length() && !bits[S.charAt(y)]; y++) {
                bits[S.charAt(y)] = true;
            }
            if (length < y - x) {
                start = x;
                end = y;
                length = y - x;
            }
            while(y

    ORIGINAL POST:

    Here is my two cents. Test strings included. boolean bits[] = new boolean[256] may be larger to encompass some larger charset.

    public static String longestUniqueString(String S) {
        int start=0, end=0, length=0;
        boolean bits[] = new boolean[256];
        int x=0, y=0;
        for(;x

提交回复
热议问题