Two strings, s and t, each of length n, that contain lowercase English characters are given as well as an integer K.
Find the maximum length of a substring of s that can be changed to the corresponding substring of t with a total cost less than or equal to K. If there is no such substring, return 0.
Example:
s = "adpgki", t = "cdmxki", K = 6.
The longest substring in s that is equal to the corresponding substring in t is s[0, 2] = t[0, 2]. Hence, the answer is 3.
Constraints:
A professional society is organizing a conference and needs to form a diverse deputation of 3 members.
The deputation must meet the following criteria:
You are given:
Your task is to calculate the total number of distinct ways to select a diverse deputation of 3 members, following the diversity rule.
Example:
m = 1
w = 3
There is m = 1 man available and there are w = 3 women. Label them m1, w1, w2, w3 for demonstration. There are 3 possible ways to form a diverse deputation: (m1, w1, w2), (m1, w1, w3) and (m1, w2, w3). The only other possible permutation is (w1, w2, w3), which does not include a man, so it is invalid.
Question 2: Diverse Deputation
We need to choose 3 people out of (m+w) total, excluding cases that don't have at least one man and one woman .
Overview
We need exactly three members with at least one man and one woman, meaning either 1 man and 2 women, or 2 men and 1 woman. We can use basic combinatorics to calculate the number of ways to pick these specific group compositions. By adding the combinations of these two mutually exclusive valid scenarios together, we mathematically guarantee our total answer.
1. The Logic Behind the Math
Think of a situation where you try to write a loop to generate every possible combination; it would be way too slow! Instead, just look at the two acceptable scenarios. To pick 2 people from any group of size N, the math formula is simply N *(N - 1) / 2.
2. Calculating the Two Scenarios
Scenario A (1 Man, 2 Women): Pick 1 man (there are m ways) and multiply by the number of ways to pick 2 women from w, which is w \times (w - 1) / 2.
Scenario B (2 Men, 1 Woman): Pick 1 woman (there are w ways) and multiply by the number of ways to pick 2 men from m, which is m \times (m - 1) / 2.
Add them together, and you have your answer in an instant!
Implementation
Logic:
Total Combinations: Calculate all possible ways to pick 3 people from the total pool:
Exclusion: Subtract groups consisting only of men or only of women.
Constraint: This ensures every valid group has at least one of each.
Same Substring Solution
Topics Involved / Prerequisites
Sliding Window / Two Pointers
Overview
Approach
1. The Sliding Window
We use two pointers,
l(left) andr(right), to represent the edges of this frame. We moverforward, calculating the absolute difference between characters and adding it to our running total cost.2. Shrinking the Frame
If adding a new character makes our total cost greater than K, we don't need to start all over. We just pull the
lside of the frame forward, subtracting the cost of the characters that fall out of the frame, until our total cost is <=K again. At each valid step, the length of our frame (r - l + 1) is a potential answer, so we just remember the biggest one we find!Psudo Code
Time Complexity
Time: O(N)- Both the
landrpointers only move forward, meaning we visit each character at most twice.Space: O(1)- We only use a few integer variables to track the cost and window size.