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
using System ; using System . Collections . Generic ; class GFG { static int longestUniqueSubsttr ( string s ) { Dictionary < char , int > seen = new Dictionary < char , int > ( ) ; int maximum_length = 0 ; int start = 0 ; for ( int end = 0 ; end < s . Length ; end ++ ) { if ( seen . ContainsKey ( s [ end ] ) ) { start = Math . Max ( start , seen [ s [ end ] ] + 1 ) ; } seen [ s [ end ] ] = end ; maximum_length = Math . Max ( maximum_length , end - start + 1 ) ; } return maximum_length ; } static void Main ( ) { string s = " geeksforgeeks " ; Console . WriteLine ( " The ▁ input ▁ string ▁ is ▁ " + s ) ; int length = longestUniqueSubsttr ( s ) ; Console . WriteLine ( " The ▁ length ▁ of ▁ the ▁ longest ▁ non - repeating ▁ character ▁ substring ▁ is ▁ " + length ) ; } }