Find maximum freqeuncy deviation of every possible substring. len(substring) >= 1
- only lower case alphabets
- give txt file
- max len of each line <= 10^4
- "aaabb" => a substring, 'aaab', has deviation 3. (3 'a's, 1 'b')
- "abcccbaa" => a substring, 'bccc', has deviation 2. (3 'c's, 1 'b')
- "aaa" => maximum answer is 0. e.g., "aa" has 2 'a's for maximum frequency, 2 'a's for minimum frequency. answer = 2 - 2 = 0
- replaced string with numbers(1, -1) for every pair of c1, c2 ranging from 'a' to 'z'
- used
Kadane's algorithm
, one of dynamic programming techniques - return maximum value
for (c1 = 'a' to 'z')
for (c2 = 'a' to 'z')
num_arr = string2num(s); // replace s with 1(c1) and -1(c2)
res = Kadane(num_arr); // apply kadane's algorithm
if (max < res) max = res; // get maximum value
return max;
C++98