Coders Packet

Longest Unique Characters Substring using unordered_maps in C++ laguage

By Jayaditya Jakkam

  • Longest Unique Characters Substring.docx
  • max lane.cpp
  • Finding the length of the longest substring that can be formed from a string with unique characters using an unordered map library in C++

    Given a string S of N length, write an efficient program to find the length of the longest substring with only unique characters. Note that S does not contain spaces.

    Code Explanation:

    In the present code, I have written a function taking string as input here I have used an unordered map library for storing the characteristics of the string.

    Firstly I have defined an unordered map named map and go through the given string and see if the character is already present in the map if yes then the start index will be updated with the maximum value of the start index and position of the index before it was present.

    Now the maximum length is updated by the maximum value of max_length, i-(start index) and after completing through the string we will be left out with the maximum length.

    Download Complete Code


    No comments yet