Finding longest substring between two same characters JavaScript


In this problem statement, our task is to find the longest substring between two same characters with the help of Javascript functionalities. This task can be done with the help of map function to keep track of the two same characters.

Logic for the given problem

The problem stated that we have to find the longest substring between two same characters in the given string. For example if we have a string like 'abcdefa', there are two same characters 'a' that have 5 characters called 'bcdef', so this will be called longest substring. For implementing the above given problem, we will use map method to keep track of first and last same characters and then show the output of substring between same characters,

Algorithm

Step 1 − Start the program by defining the function longestSubstring and pass an argument ‘str’.

Step 2 − After that we will declare variables called maxLength and begin, initialize these variables with 0 and -1 respectively.

Step 3 − Now we will create a map called charMap with the help of Map function and new keyword.

Step 4 − At this step we will start a for loop and run this loop till the length of the string str.

Step 5 − After all the above steps, create another variable to get every character details from the string and to check the conditions.

Step 6 − As we get every character in the char variable, its time to check that the character has been seen before or not. If the given condition is true then add it in the previous index value.

Step 7 − After checking all the above conditions we will get the substring which belongs between the same characters present in the string and show the output to the console.

Code for the algorithm

//function to find the longest substring
function longestSubstring(str) {
   let maxLength = 0;
   let begin = -1;
   let charMap = new Map();
   for (let i = 0; i < str.length; i++) {
      const char = str[i]; 
      // condition for checking the character has seen before
      if (charMap.has(char)) {
         const previousIndex = charMap.get(char);
         const len = i - previousIndex - 1;
         if (len > maxLength) {
            maxLength = len;
            begin = previousIndex + 1;
         }
      } else {
         charMap.set(char, i);
      }
   }
   if (begin === -1) {
      return "";
   }
   return str.substring(begin, begin + maxLength);
}
const str = "abcaabcdaaaefghij";
const longSubstr = longestSubstring(str);
console.log("The longest substring between same characters: ");
console.log(longSubstr);

Complexity

Let's assume that n is the length of the input string str so the time taken to execute the above function is O(n). Because the function iterates over the string only one time and processes constant time actions for every character. Let's say m is the number of characters between the same character in the input string so the space complexity for the code is O(m). Because the code is using a map function to store the index at which every character is seen and the size of this is equal to the size of unique characters.

Conclusion

As we have seen how to use Javascript functions to identify the longest substring between two same characters. Also the method we have implemented above effectively gets the substring from the input string.

Updated on: 18-May-2023

347 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements