Given a string S consisting of characters from āaā to āzā. The task is to find the length of the largest substring of S which contains a character whose frequency in the sub-string is greater than or equal to half of the length of the substring.
Note: For odd-length substring, to calculate half-length consider integer division. For example, half of 11 is 5.
Examples:Ā
Input : S = "ababbbacbcbcca" Output : 13 Substring from index 1(0-based indexing) to index 13, "babbbacbcbcca" have b's frequency equals to 6 which is equal to half of the length of substring (13/2 = 6). Input : S = "abcde" Output : 3
Simple Approach: The idea is to find all the substring of S and for each substring find the frequency of each character and compare it with half of the length of the substring. Now, among all substrings satisfying the condition, the output is the length of the one having the largest length.
Ā
Efficient Approach:Ā
First, observe there can be only 26 distinct characters in the string S. So, consider each character one by one for having a frequency greater than or equal to the length of the substring.
So, to find the length of the longest possible sub-string which satisfies the condition, for each character we will make the prefix array of the count of the character. For example, for S = āabacdaefā, the prefix array for āaā will be, say freq[], [1, 1, 2, 2, 2, 3, 3, 3].Ā
We are looking for the following conditions to satisfy:Ā
freq[r] - freq[l - 1] >= (r - l)/2, where l, r are the indices.
Also, we can write,Ā
(2 * freq[r]) - r >= (2 * freq[l - 1]) - l
So, find two arrays r[] and l[], where r[i] = (2 * freq[i]) ā i and l[i] = (2 * freq[l ā 1]) ā l, for 1 <= i <= length of S.
Now, for every i in r[], find the lower bound in l[] using binary search such that the index is minimum in l[], say j.
So, i ā j + 1 is one of the solutions. Find the maximum one.
Then loop the above method for each character.
Below is the implementation of the above approach:Ā
C++
// C++ implementation of the above approach Ā
#include <bits/stdc++.h> using namespace std; Ā
// Function to return the length of the longest // sub string having frequency of a character // greater than half of the length of the // sub string int maxLength(string s, int n) { Ā Ā Ā Ā int ans = INT_MIN; Ā Ā Ā Ā vector< int > A, L, R; Ā Ā Ā Ā int freq[n + 5]; Ā
Ā Ā Ā Ā // for each of the character 'a' to 'z' Ā Ā Ā Ā for ( int i = 0; i < 26; i++) { Ā Ā Ā Ā Ā Ā Ā Ā int count = 0; Ā Ā Ā Ā Ā Ā Ā Ā memset (freq, 0, sizeof freq); Ā
Ā Ā Ā Ā Ā Ā Ā Ā // finding frequency prefix array of the Ā Ā Ā Ā Ā Ā Ā Ā // character Ā Ā Ā Ā Ā Ā Ā Ā for ( int j = 0; j < n; j++) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (s[j] - 'a' == i) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā count++; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā freq[j] = count; Ā Ā Ā Ā Ā Ā Ā Ā } Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Finding the r[] and l[] arrays. Ā Ā Ā Ā Ā Ā Ā Ā for ( int j = 0; j < n; j++) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā L.push_back((2 * freq[j - 1]) - j); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā R.push_back((2 * freq[j]) - j); Ā Ā Ā Ā Ā Ā Ā Ā } Ā
Ā Ā Ā Ā Ā Ā Ā Ā int max_len = INT_MIN; Ā Ā Ā Ā Ā Ā Ā Ā int min_val = INT_MAX; Ā
Ā Ā Ā Ā Ā Ā Ā Ā // for each j from 0 to n Ā Ā Ā Ā Ā Ā Ā Ā for ( int j = 0; j < n; j++) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā min_val = min(min_val, L[j]); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā A.push_back(min_val); Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int l = 0, r = j; Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Finding the lower bound of i. Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā while (l <= r) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int mid = (l + r) >> 1; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (A[mid] <= R[j]) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā max_len = max(max_len, j - mid + 1); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā r = mid - 1; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā l = mid + 1; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā } Ā
Ā Ā Ā Ā Ā Ā Ā Ā // storing the maximum value of i - j + 1 Ā Ā Ā Ā Ā Ā Ā Ā ans = max(ans, max_len); Ā
Ā Ā Ā Ā Ā Ā Ā Ā // clearing all the vector so that it clearing Ā Ā Ā Ā Ā Ā Ā Ā // be use for other character. Ā Ā Ā Ā Ā Ā Ā Ā A.clear(); Ā Ā Ā Ā Ā Ā Ā Ā R.clear(); Ā Ā Ā Ā Ā Ā Ā Ā L.clear(); Ā Ā Ā Ā } Ā
Ā Ā Ā Ā return ans; } Ā
// Driver Code int main() { Ā Ā Ā Ā string s = "ababbbacbcbcca" ; Ā Ā Ā Ā int n = s.length(); Ā
Ā Ā Ā Ā cout << maxLength(s, n) << '\n' ; Ā Ā Ā Ā Ā Ā Ā Ā Ā return 0; } |
Java
// Java implementation of the above approach import java.util.*; Ā
class GFG { Ā
// Function to return the length of the longest // sub string having frequency of a character // greater than half of the length of the // sub string static int maxLength(String s, int n) { Ā Ā Ā Ā int ans = Integer.MIN_VALUE; Ā Ā Ā Ā Vector<Integer> A = new Vector<Integer>(); Ā Ā Ā Ā Vector<Integer> L = new Vector<Integer>(); Ā Ā Ā Ā Vector<Integer> R = new Vector<Integer>(); Ā
Ā Ā Ā Ā int []freq = new int [n + 5 ]; Ā
Ā Ā Ā Ā // for each of the character 'a' to 'z' Ā Ā Ā Ā for ( int i = 0 ; i < 26 ; i++) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā int count = 0 ; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // finding frequency prefix array of the Ā Ā Ā Ā Ā Ā Ā Ā // character Ā Ā Ā Ā Ā Ā Ā Ā for ( int j = 0 ; j < n; j++) Ā Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (s.charAt(j) - 'a' == i) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā count++; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā freq[j] = count; Ā Ā Ā Ā Ā Ā Ā Ā } Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Finding the r[] and l[] arrays. Ā Ā Ā Ā Ā Ā Ā Ā for ( int j = 1 ; j < n; j++) Ā Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā L.add(( 2 * freq[j - 1 ]) - j); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā R.add(( 2 * freq[j]) - j); Ā Ā Ā Ā Ā Ā Ā Ā } Ā
Ā Ā Ā Ā Ā Ā Ā Ā int max_len = Integer.MIN_VALUE; Ā Ā Ā Ā Ā Ā Ā Ā int min_val = Integer.MAX_VALUE; Ā
Ā Ā Ā Ā Ā Ā Ā Ā // for each j from 0 to n Ā Ā Ā Ā Ā Ā Ā Ā for ( int j = 0 ; j < L.size(); j++) Ā Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā min_val = Math.min(min_val, L.get(j)); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā A.add(min_val); Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int l = 0 , r = j; Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Finding the lower bound of i. Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā while (l <= r) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int mid = (l + r) >> 1 ; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (A.get(mid) <= R.get(j)) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā max_len = Math.max(max_len, Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā j - mid + 1 ); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā r = mid - 1 ; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā l = mid + 1 ; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā } Ā
Ā Ā Ā Ā Ā Ā Ā Ā // storing the maximum value of i - j + 1 Ā Ā Ā Ā Ā Ā Ā Ā ans = Math.max(ans, max_len); Ā
Ā Ā Ā Ā Ā Ā Ā Ā // clearing all the vector so that it clearing Ā Ā Ā Ā Ā Ā Ā Ā // be use for other character. Ā Ā Ā Ā Ā Ā Ā Ā A.clear(); Ā Ā Ā Ā Ā Ā Ā Ā R.clear(); Ā Ā Ā Ā Ā Ā Ā Ā L.clear(); Ā Ā Ā Ā } Ā Ā Ā Ā return ans; } Ā
// Driver Code public static void main(String[] args) { Ā Ā Ā Ā String s = "ababbbacbcbcca" ; Ā Ā Ā Ā int n = s.length(); Ā
Ā Ā Ā Ā System.out.println(maxLength(s, n)); } } Ā
// This code is contributed by Princi Singh |
Python3
# Python3 implementation of the above approach import sys Ā
# Function to return the length # of the longest sub string # having frequency of a character # greater than half of the length # of the sub string def maxLength(s, n) : Ā Ā Ā Ā Ā Ā Ā Ā Ā ans = - (sys.maxsize + 1 ); Ā Ā Ā Ā A, L, R = [], [], []; Ā Ā Ā Ā freq = [ 0 ] * (n + 5 ); Ā
Ā Ā Ā Ā # for each of the character 'a' to 'z' Ā Ā Ā Ā for i in range ( 26 ) : Ā Ā Ā Ā Ā Ā Ā Ā count = 0 ; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # finding frequency prefix array Ā Ā Ā Ā Ā Ā Ā Ā # of the character Ā Ā Ā Ā Ā Ā Ā Ā for j in range (n) : Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if ( ord (s[j]) - ord ( 'a' ) = = i) : Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā count + = 1 ; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā freq[j] = count; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # Finding the r[] and l[] arrays. Ā Ā Ā Ā Ā Ā Ā Ā for j in range (n) : Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā L.append(( 2 * freq[j - 1 ]) - j); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā R.append(( 2 * freq[j]) - j); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā max_len = - (sys.maxsize + 1 ); Ā Ā Ā Ā Ā Ā Ā Ā min_val = sys.maxsize ; Ā
Ā Ā Ā Ā Ā Ā Ā Ā # for each j from 0 to n Ā Ā Ā Ā Ā Ā Ā Ā for j in range (n) : Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā min_val = min (min_val, L[j]); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā A.append(min_val); Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā l = 0 ; r = j; Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # Finding the lower bound of i. Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā while (l < = r) : Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā mid = (l + r) >> 1 ; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (A[mid] < = R[j]) : Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā max_len = max (max_len, j - mid + 1 ); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā r = mid - 1 ; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else : Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā l = mid + 1 ; Ā
Ā Ā Ā Ā Ā Ā Ā Ā # storing the maximum value of i - j + 1 Ā Ā Ā Ā Ā Ā Ā Ā ans = max (ans, max_len); Ā
Ā Ā Ā Ā Ā Ā Ā Ā # clearing all the vector so that it can Ā Ā Ā Ā Ā Ā Ā Ā # be used for other characters. Ā Ā Ā Ā Ā Ā Ā Ā A.clear(); Ā Ā Ā Ā Ā Ā Ā Ā R.clear(); Ā Ā Ā Ā Ā Ā Ā Ā L.clear(); Ā
Ā Ā Ā Ā return ans; Ā
# Driver Code if __name__ = = "__main__" : Ā
Ā Ā Ā Ā s = "ababbbacbcbcca" ; Ā Ā Ā Ā n = len (s); Ā
Ā Ā Ā Ā print (maxLength(s, n)); Ā
# This code is contributed by AnkitRai01 |
C#
// C# implementation of the above approach using System; using System.Collections.Generic; Ā
class GFG { Ā
// Function to return the length of the longest // sub string having frequency of a character // greater than half of the length of the // sub string static int maxLength(String s, int n) { Ā Ā Ā Ā int ans = int .MinValue; Ā Ā Ā Ā List< int > A = new List< int >(); Ā Ā Ā Ā List< int > L = new List< int >(); Ā Ā Ā Ā List< int > R = new List< int >(); Ā
Ā Ā Ā Ā int []freq = new int [n + 5]; Ā
Ā Ā Ā Ā // for each of the character 'a' to 'z' Ā Ā Ā Ā for ( int i = 0; i < 26; i++) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā int count = 0; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // finding frequency prefix array of the Ā Ā Ā Ā Ā Ā Ā Ā // character Ā Ā Ā Ā Ā Ā Ā Ā for ( int j = 0; j < n; j++) Ā Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (s[j] - 'a' == i) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā count++; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā freq[j] = count; Ā Ā Ā Ā Ā Ā Ā Ā } Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Finding the r[] and l[] arrays. Ā Ā Ā Ā Ā Ā Ā Ā for ( int j = 1; j < n; j++) Ā Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā L.Add((2 * freq[j - 1]) - j); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā R.Add((2 * freq[j]) - j); Ā Ā Ā Ā Ā Ā Ā Ā } Ā
Ā Ā Ā Ā Ā Ā Ā Ā int max_len = int .MinValue; Ā Ā Ā Ā Ā Ā Ā Ā int min_val = int .MaxValue; Ā
Ā Ā Ā Ā Ā Ā Ā Ā // for each j from 0 to n Ā Ā Ā Ā Ā Ā Ā Ā for ( int j = 0; j < L.Count; j++) Ā Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā min_val = Math.Min(min_val, L[j]); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā A.Add(min_val); Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int l = 0, r = j; Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Finding the lower bound of i. Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā while (l <= r) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int mid = (l + r) >> 1; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (A[mid] <= R[j]) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā max_len = Math.Max(max_len, Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā j - mid + 1); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā r = mid - 1; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā l = mid + 1; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā } Ā
Ā Ā Ā Ā Ā Ā Ā Ā // storing the maximum value of i - j + 1 Ā Ā Ā Ā Ā Ā Ā Ā ans = Math.Max(ans, max_len); Ā
Ā Ā Ā Ā Ā Ā Ā Ā // clearing all the vector so that it can Ā Ā Ā Ā Ā Ā Ā Ā // be used for other characters. Ā Ā Ā Ā Ā Ā Ā Ā A.Clear(); Ā Ā Ā Ā Ā Ā Ā Ā R.Clear(); Ā Ā Ā Ā Ā Ā Ā Ā L.Clear(); Ā Ā Ā Ā } Ā Ā Ā Ā return ans; } Ā
// Driver Code public static void Main(String[] args) { Ā Ā Ā Ā String s = "ababbbacbcbcca" ; Ā Ā Ā Ā int n = s.Length; Ā
Ā Ā Ā Ā Console.WriteLine(maxLength(s, n)); } } Ā
// This code is contributed by 29AjayKumar |
Javascript
<script> Ā Ā Ā Ā Ā Ā // JavaScript implementation of the above approach Ā Ā Ā Ā Ā Ā // Function to return the length of the longest Ā Ā Ā Ā Ā Ā // sub string having frequency of a character Ā Ā Ā Ā Ā Ā // greater than half of the length of the Ā Ā Ā Ā Ā Ā // sub string Ā Ā Ā Ā Ā Ā function maxLength(s, n) { Ā Ā Ā Ā Ā Ā Ā Ā var ans = -2147483648; Ā Ā Ā Ā Ā Ā Ā Ā var A = []; Ā Ā Ā Ā Ā Ā Ā Ā var L = []; Ā Ā Ā Ā Ā Ā Ā Ā var R = []; Ā
Ā Ā Ā Ā Ā Ā Ā Ā var freq = new Array(n + 5).fill(0); Ā
Ā Ā Ā Ā Ā Ā Ā Ā // for each of the character 'a' to 'z' Ā Ā Ā Ā Ā Ā Ā Ā for ( var i = 0; i < 26; i++) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā var count = 0; Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // finding frequency prefix array of the Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // character Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for ( var j = 0; j < n; j++) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (s[j].charCodeAt(0) - "a" .charCodeAt(0) === i) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā count++; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā freq[j] = count; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Finding the r[] and l[] arrays. Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for ( var j = 1; j < n; j++) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā L.push(2 * freq[j - 1] - j); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā R.push(2 * freq[j] - j); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā var max_len = -2147483648; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā var min_val = 2147483648; Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // for each j from 0 to n Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for ( var j = 0; j < L.length; j++) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā min_val = Math.min(min_val, L[j]); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā A.push(min_val); Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā var l = 0, Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā r = j; Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Finding the lower bound of i. Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā while (l <= r) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā var mid = (l + r) >> 1; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (A[mid] <= R[j]) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā max_len = Math.max(max_len, j - mid + 1); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā r = mid - 1; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā l = mid + 1; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // storing the maximum value of i - j + 1 Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ans = Math.max(ans, max_len); Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // clearing all the vector so that it can Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // be used for other characters. Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā A = []; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā R = []; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā L = []; Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā return ans; Ā Ā Ā Ā Ā Ā } Ā
Ā Ā Ā Ā Ā Ā // Driver Code Ā Ā Ā Ā Ā Ā var s = "ababbbacbcbcca" ; Ā Ā Ā Ā Ā Ā var n = s.length; Ā
Ā Ā Ā Ā Ā Ā document.write(maxLength(s, n)); </script> |
13
Ā
Time Complexity: O(26*N*logN)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!