Cognizant Coding Question – Solved

7 Live
A substring is a group of contiguous characters in a string. For instance, all substrings of abc are [a, b, c, ab, bc, abc]. Given a binary representation of a number, determine the total number of substrings present that match the following conditions: 1. The 0s and 1s are grouped consecutively (e.g., 01, 10, 0011, 1100, 000111, etc.). 2. The number of 0s in the substring is equal to the number of 1s in the substring. Example: s = 001101 The 4 substrings matching the two conditions include [0011, 01, 10, 01]. Note that 01 appears twice, from indices 1-2 and 4-5. There are other substrings, e.g., 001 and 011 that match the first condition but not the second. Function Description: Complete the function `counting` in the editor below. counting has the following parameter(s): string s: a string representation of a binary integer

Asked in: Cognizant

Image of the Question

Question Image

All Testcases Passed ✔



Passcode Image

Solution


static int counting(String s) {
        int n = s.length();
        List<Integer> counts = new ArrayList<>();
        int i = 0;
// ... rest of solution available after purchase

🔒 Please login to view the solution

Explanation


```
To solve the problem of counting substrings in a binary string that satisfy two conditions—(1) zeros and ones are grouped consecutively, and (2) the count of zeros equals the count of ones—we need a clear strategy that breaks down the problem into manageable parts.

1. Understanding the Problem:
The substrings we're interested in are those where all zeros appear together consecutively, followed immediately by an equal number of consecutive ones, or vice versa. For example, valid substrings include "01", "10", "0011", "1100", "000111", etc. Invalid examples are "001" or "011" because even though zeros and ones are grouped consecutively, the counts are not equal.

2. Key Insight: Consecutive Groups
To satisfy the condition that zeros and ones are grouped consecutively, the substring must be composed of exactly two groups:
- One group of zeros followed immediately by a group of ones, or
- One group of ones followed immediately by a group of zeros.
The length of these two groups must be equal.

For example, consider the string "001101":
- The substring "0011" is formed by two groups: "00" (zeros) and "11" (ones), both length 2.
- The substring "01" is "0" followed by "1", both length 1.
- The substring "10" is "1" followed by "0", both length 1.
- The substring "01" again at a different position.

3. Breaking the String into Groups:
A useful approach is to traverse the string and record the lengths of consecutive groups of identical characters. For example, "001101" breaks down into groups:
- "00" length 2 (zeros)
- "11" length 2 (ones)
- "0" length 1 (zero)
- "1" length 1 (one)

4. Counting Valid Substrings Using Group Lengths:
Once we have the consecutive group lengths, the problem reduces to:
- For every adjacent pair of groups (group[i], group[i+1]), the number of valid substrings formed is the minimum of their lengths.
Why? Because a substring must have equal length groups of zeros and ones, so for groups of lengths l1 and l2, the valid substrings that can be formed are all substrings of length 2*k where k ranges from 1 to min(l1, l2).
For example, if one group has length 3 and the next has length 2, valid substrings include those of length 2 (1 zero + 1 one) and length 4 (2 zeros + 2 ones), totaling min(3, 2) = 2 substrings.

5. Step-By-Step Method:
- Iterate through the string once, maintaining a count of the current character group length.
- When the character changes, record the count in a list and reset for the next group.
- After the iteration, you will have an array of group lengths.
- Iterate through the array of group lengths, sum the minimum of each adjacent pair.

6. Edge Cases and Validations:
- A string consisting entirely of zeros or entirely of ones will have no valid substrings because there is no adjacent group of the opposite character.
- Very short strings like "0" or "1" have zero valid substrings.
- Repeated sequences of alternating characters (e.g., "010101") will have many valid substrings, each corresponding to pairs of adjacent groups of length 1.

7. Time Complexity Considerations:
- The approach requires a single pass to form the groups (O(n)) and a second pass to sum minimums of adjacent groups (O(n)).
- Overall complexity is O(n), efficient for large strings.

8. Summary of the Approach:
- Transform the problem into counting consecutive character groups.
- Use the lengths of these groups to count valid substrings by summing the minimum lengths of adjacent groups.
- This approach leverages the nature of the problem's constraints and avoids the need to check all substrings explicitly, which would be inefficient.

By thinking of the string as a sequence of runs of zeros and ones and then using these runs to count valid substrings based on adjacent pairs, you arrive at a clean, efficient solution that neatly addresses both problem conditions.
```


Related Questions