Back to photostream

Largest Substring Between Two Equal Characters Leetcode Solution

The problem Largest Substring Between Two Equal Characters Leetcode Solution, asks us to find the length of the largest substring. Here, a condition is imposed on the substring. The substring should be between the same characters. So, the string must contain at least two equal characters so that the output happens to be a natural number else -1 is returned. But before moving ahead with the solution let us take a look at a few examples.

 

s = "aa"

0

Explanation: The input contains two 'a's and the string between them happens to be the longest substring satisfying the imposed conditions. Thus, the output is correct.

s = "abca"

2

Explanation: There exists only a single character having at least two instances in the input string. So, the optimal output will contain "bc".

Approach for Largest Substring Between Two Equal Characters Leetcode Solution

The solution for the problem Largest Substring Between Two Equal Characters Leetcode Solution is easy to understand. In the problem, we are only asked to find the length of the largest substring but not the string itself. So, we simply create two arrays that store the first and last index of any character. Initially, we fill these arrays with -1 which denotes no occurrence of a character. Once we find a character we store the index in the first array if it is filled with -1. If that does not have -1, we store the index in the second array.

 

Using these two arrays, we find the maximum length.

 

www.tutorialcup.com/leetcode-solutions/largest-substring-...

22 views
0 faves
0 comments
Uploaded on August 30, 2021