Hacktoberfest - first 5 PR - part 4

4th PR -  longestUniqueSubstring

Description from the issue:

Understand The Problem 

These requirements can be taken from the first sentence of the description above:
  • Must be a substring from the given string
  • Must be continuous
  • Must not contain duplicated characters
  • Must be the longest one (since with just 3 requirements above can make multiple outputs)
However let's take a look at example 2, with the requirements above, aabccdefgha should generate the output cdefgha, but instead it has cdefgh

From that we can clarify that the requirement "Must be continuous" is supposed to talk about the alphabet.

The Solution

Now we already know the concept, these are the steps in order to be done:
  • Process through all characters in the string
    1. Check if the next character is the following character in the alphabet (req: continuous)
    2. If true, meaning the substring might continue further, so skip to next loop
    3. If not, the current position is divided to get a substring. Check if the substring is the longest one
    4. Continue til the end of string
  • Return the longest substring remain.
So the final code would look like this:

Link:

Comments

Popular Posts