Write a function:class Solution { public int solution(String S); }that, given a string S consisting of N lowercase English letters, returns the length of the longest substring in which every letter occurs an even number of times. A substring is defined as a contiguous segment of a string. If no such substring exists, return 0.Examples:1. Given S = "bdaaadadb", the function should return 6. Substrings in which every letter occurs an even number of times are "aa", "adad", "daaada" and "aaadad". The length of the longest of them is 6.2. Given S = "abacb", the function should return 0. There is no non-empty substring in which every letter occurs an even number of times.3. Given S = "zthtzh", the function should return 6. Every letter in the whole string occurs an even number of times.Write an efficient algorithm for the following assumptions:N is an integer within the range [1..100,000];string S consists only of lowercase letters ('a'-'z').
Question
Write a function:class Solution { public int solution(String S); }that, given a string S consisting of N lowercase English letters, returns the length of the longest substring in which every letter occurs an even number of times. A substring is defined as a contiguous segment of a string. If no such substring exists, return 0.Examples:1. Given S = "bdaaadadb", the function should return 6. Substrings in which every letter occurs an even number of times are "aa", "adad", "daaada" and "aaadad". The length of the longest of them is 6.2. Given S = "abacb", the function should return 0. There is no non-empty substring in which every letter occurs an even number of times.3. Given S = "zthtzh", the function should return 6. Every letter in the whole string occurs an even number of times.Write an efficient algorithm for the following assumptions:N is an integer within the range [1..100,000];string S consists only of lowercase letters ('a'-'z').
Solution 1
Here is a Python solution for the problem:
class Solution:
def solution(self, S):
N = len(S)
result = 0
prefix_sum = [[0]*26]
for i in range(N):
current_sum = prefix_sum[i][:]
current_sum[ord(S[i]) - ord('a')] += 1
prefix_sum.append(current_sum)
for i in range(N):
for j in range(i+1, N+1, 2):
if all((prefix_sum[j][k] - prefix_sum[i][k]) % 2 == 0 for k in range(26)):
result = max(result, j - i)
return result
This solution works by maintaining a prefix sum of the counts of each character in the string. For each pair of indices (i, j), it checks if the count of each character in the substring S[i:j] is even. If it is, it updates the result with the maximum length of such substring. The time complexity of this solution is O(N^3), which may not be efficient for large inputs.
A more efficient solution would be to use a bitmask to keep track of the characters with odd counts in the prefix of the string. When we encounter a prefix with the same bitmask, it means that all characters in the substring between these two prefixes have even counts. Here is the optimized solution:
class Solution:
def solution(self, S):
N = len(S)
seen = [-1] + [N] * (1 << 26)
result = 0
mask = 0
for i in range(N):
mask ^= 1 << (ord(S[i]) - ord('a'))
for submask in range(mask+1):
if mask & submask == submask:
seen[submask] = min(seen[submask], i)
result = max(result, i - seen[mask])
return result
This solution works in O(N) time, which is much more efficient for large inputs.
Solution 2
Here is a Python solution for the problem:
class Solution:
def solution(self, S):
N = len(S)
result = 0
prefix_sum = [[0]*26]
for i in range(N):
new_prefix_sum = prefix_sum[-1][:]
new_prefix_sum[ord(S[i]) - ord('a')] += 1
prefix_sum.append(new_prefix_sum)
for i in range(N):
for j in range(i+result, N):
if all((prefix_sum[j+1][k] - prefix_sum[i][k]) % 2 == 0 for k in range(26)):
result = max(result, j-i+1)
return result
This solution works by maintaining a prefix sum of the counts of each character in the string. For each possible start of the substring, it then checks the longest possible end of the substring where all characters occur an even number of times. This is done by checking if the count of each character in the substring (which can be calculated using the prefix sum) is even. The time complexity of this solution is O(N^2), which may be too slow if N is close to 100,000. However, the problem does not specify a required time complexity, so this solution is acceptable.
Similar Questions
A substring is a contiguous sequence of characters within a string. Given a string S and an integer n, write a C code to generate all substrings of length n without vowels. For example, if the given string S1 is ‘apple’ then substrings of S1 of length 3 are app, ppl, pple. And substrings of length 3 without vowels is only one and it is ppl.
Given a string s, find the length of the longest substring without repeating characters.
Given a string s, find the length of the longest substring without repeating characters. Example 1:Input: s = "abcabcbb"Output: 3Explanation: The answer is "abc", with the length of 3.Example 2:Input: s = "bbbbb"Output: 1Explanation: The answer is "b", with the length of 1.Example 3:Input: s = "pwwkew"Output: 3Explanation: The answer is "wke", with the length of 3.Notice that the answer must be a substring, "pwke" is a subsequence and not a substring. Constraints:0 <= s.length <= 5 * 104s consists of English letters, digits, symbols and spaces.
Given a string S consisting of only lowercase characters, find the length of the longest substring that does not contain any vowel.Input FormatFirst line of input contains T - number of test cases. Its followed by T lines, each line contains a string of size N, containing only lowercase english alphabets.Output FormatFor each test case, print the length of the largest substring that does not contain any vowel, separated by newline.Constraints1 <= T <= 1001 <= N <= 104
Internship Alert!New month-> Fresh Chance to top the leaderboard and get SDE Internship! Given a string s of length n, find the longest repeating non-overlapping substring in it. In other words find 2 identical substrings of maximum length which do not overlap. Return the longest non-overlapping substring. Return "-1" if no such string exists.Note: Multiple Answers are possible but you have to return the substring whose first occurrence is earlier.For Example: "abhihiab", here both "ab" and "hi" are possible answers. But you will have to return "ab" because it's first occurrence appears before the first occurrence of "hi".
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.