Length of the longest substring without repeating characters | Creating a set to store the last positions of occurrence ; starting the initial point of window to index 0 ; Checking if we have already seen the element or not ; If we have seen the number , move the start pointer to position after the last occurrence ; Updating the last seen value of the character ; Driver code
import java . util . * ; class GFG { static int longestUniqueSubsttr ( String s ) { HashMap < Character , Integer > seen = new HashMap < > ( ) ; int maximum_length = 0 ; int start = 0 ; for ( int end = 0 ; end < s . length ( ) ; end ++ ) { if ( seen . containsKey ( s . charAt ( end ) ) ) { start = Math . max ( start , seen . get ( s . charAt ( end ) ) + 1 ) ; } seen . put ( s . charAt ( end ) , end ) ; maximum_length = Math . max ( maximum_length , end - start + 1 ) ; } return maximum_length ; } public static void main ( String [ ] args ) { String s = " geeksforgeeks " ; System . out . println ( " The ▁ input ▁ String ▁ is ▁ " + s ) ; int length = longestUniqueSubsttr ( s ) ; System . out . println ( " The ▁ length ▁ of ▁ the ▁ longest ▁ non - repeating ▁ character ▁ substring ▁ is ▁ " + length ) ; } }