[虚拟机]Intelligent Substring 智能字符串


You are exploring a new language where there are two types of characters: special and normal. A character is special if its value is 1 and normal if its value is 0.

Given a string s, and an integer k, determine the length of the longest possible substring with at most k normal characters. There will be a 26 digit bit string charValue where each position represents the special or normal nature, or value, of the corresponding letter of the English alphabet.

For example, if s = "abcde" and k = 2 , using the following charValue:

alphabet =  abcdefghijklmnopqrstuvwxyz 
charValue = 10101111111111111111111111

the normal characters are in the set {b, d}. Since k is 2 and there are only two normal characters present in the string, any substring meets the criterion. The longest substring can be made up of the entire string and has a length of 5. If instead k = 1, possible substrings are ['b', 'd', 'ab', 'bc', 'cd', 'de', 'abc', 'cdel. The longest substrings are 3 characters long.

Function Description
Complete the function getSpecialSubstring in the editor below. The function must return an integer that denotes the length of the longest substring of s with at most k normal characters.

getSpecialSubstring has the following parameter(s):
s: the input string
k: the maximum number of normal characters allowed in a substring
charValue: a string representing special or normal for each letter of the alphabet, ascii[a-zJ

Constraints
• 1 ≤ |s| ≤ 10
• 1 ≤ k ≤ |S|
• |charValue| = 26
• charValue[i] €  {0,1}

题意:

思路:

代码:

猜你喜欢

转载自www.cnblogs.com/liuliu5151/p/11512973.html