Longest Substring Without Repeating Character

The Longest Substring Without Repeating Character is the maximum substring or part of a big string where no character is repeated. 

For example in the string WeSome.Org

SubString Without Repetition  Length
wesom 5
some. 5
me.org 6

so the length of the Longest Substring Without Repeating Character is 6.

In the string iamlearningdsalgofromwesome.org

SubString Without Repetition  Length
mlearni 7
ingdsal 7
dsalgofr 8

so the length of the Longest Substring Without Repeating Character is 8.

In the string longestuniquesubstring

SubString Without Repetition  Length
longestu 8
gestuniq 8
ubstring 8

so the length of the Longest Substring Without Repeating Character is 8.

package org.wesome.dsalgo;

public class LongestUniqueSubString {
    public static int longestUniqueSubString(String str) {
        String window = "";

        int maxLength = -1;
        if (str.isEmpty()) {
            return 0;
        } else if (str.length() == 1) {
            return 1;
        }
        for (char c : str.toLowerCase().toCharArray()) {
            String current = String.valueOf(c);

            // If string already contains the character
            // Then substring after the repeated character
            if (window.contains(current)) {
                window = window.substring(window.indexOf(current) + 1);
            }
            window = window + c;
            maxLength = Math.max(window.length(), maxLength);
        }
        return maxLength;
    }
}
package org.wesome.dsalgo;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class LongestUniqueSubStringTest {
    @Test
    void longestUniqueSubStringTest1() {
        String string = "WeSome.Org";
        Assertions.assertEquals(6, LongestUniqueSubString.longestUniqueSubString(string));
    }

    @Test
    void longestUniqueSubStringTest2() {
        String string = "iAmLearningDsAlgoFromWeSome.Org";
        Assertions.assertEquals(8, LongestUniqueSubString.longestUniqueSubString(string));
    }

    @Test
    void longestUniqueSubStringTest3() {
        String string = "LongestUniqueSubString";
        Assertions.assertEquals(8, LongestUniqueSubString.longestUniqueSubString(string));
    }
}
plugins {
    id 'java'
}

group = 'org.wesome'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = JavaVersion.VERSION_1_8

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter:5.6.2'
}

test {
    useJUnitPlatform()
}

follow us on