Imagine you are working on a text processing feature for a text editor that needs to find the longest substring within a given text. You need to implement two functions:
- One to find the longest substring without repeating characters.
- One to find the longest substring with at most two repeating characters.
-
Function Signature:
public int LongestSubstringWithoutRepetition(string s); public int LongestSubstringWithTwoRepeatingCharacters(string s);
-
Input:
s
: A string representing the text to be processed.
-
Output:
- Return an integer representing the length of the longest substring that meets the criteria.
-
Constraints:
- The input string
s
consists of English letters, digits, symbols, and spaces.
- The input string
Extra Credit:
- Modify the function to handle Unicode characters efficiently.
Example:
string s = "abcabcbb";
int resultWithoutRepetition = LongestSubstringWithoutRepetition(s); // Expected output: 3
int resultWithTwoRepeating = LongestSubstringWithTwoRepeatingCharacters(s); // Expected output: 7
The function should return:
int resultWithoutRepetition = LongestSubstringWithoutRepetition(s);
// Expected output: 3
int resultWithTwoRepeating = LongestSubstringWithTwoRepeatingCharacters(s);
// Expected output: 7
Hints:
- Use a sliding window approach to maintain the current substring.
- Use a hash set or hash map to track characters in the current window.
Sample Dataset:
string s = "pwwkew";
Expected Output for Sample Dataset:
int resultWithoutRepetition = LongestSubstringWithoutRepetition(s);
// Expected output: 3
int resultWithTwoRepeating = LongestSubstringWithTwoRepeatingCharacters(s);
// Expected output: 4