See the question on Hacker Earth
Difficulty Level: Easy Problem You live in a village. The village can be represented as a line that contains n grids. Each grid can be denoted as a house that is marked as H or a blank space that is marked as . A person lives in each house. A person can move to a grid if it is adjacent to that person. Therefore, the grid must be present on the left and right side of that person. Now, you are required to put some fences that can be marked as B on some blank spaces so that the village can be divided into several pieces. A person cannot walk past a fence but can walk through a house. You are required to divide the house based on the following rules: A person cannot reach a house that does not belong to that specific person. The number of grids each person can reach is the same and it includes the grid in which the house is situated. In order to show that you are enthusiastic and if there are many answers, then you are required to print the one where most fences are placed. Your task is to decide whether there is a possible solution. Print the possible solution. Input format First line: An integer n that represents the number of grids (1 <= n < 20) Second line: n characters that indicate the villages that are represented as H or . Output format The output must be printed in the following format: First line: If possible, then print YES. Otherwise, print NO. Second line: If the answer is YES, then print the way to do so. Sample Input 5 H...H Sample Output YES HBBBH Time Limit: 1 Memory Limit: 256 Source Limit: Explanation Each person can reach 1 grid. Each person can reach his own houses only. Note that HB.BH also works. Each person can reach only 1 grid. But H..BH does not work. Because the first person can reach 3 grids but the second one can only reach 1. H...H does not work either. The first person can reach the second person's house which is bad. So you need to print HBBBH because it has the most fences.
A quick and rough solution would be: 1. Check if pattern HH is present in the input string. If it is, print NO and return. 2. If the pattern HH is not present, replace . with B and return the new string. A better solution would be: 1. Start iteration over the characters of the string. 2. If the character is H, check if the next character is also H. If it is, print NO and return. 3. If the next character is not H, check if it is a dot. If it is, replace it with B.Critique of these two solutions:
You've clearly understood the Split House problem and proposed two reasonable solutions. Here's a full evaluation and critique of your approaches, along with improvements and edge case considerations.
✅ Problem Summary
Given a string with H
(houses) and .
(empty spaces), we must:
-
Prevent any two adjacent
H
s — no person should be able to access another's house. -
Replace every
.
withB
to place fences. -
Maximize the number of
B
s (prefer the configuration with the most fences). -
Output
YES
and the final configuration if possible, otherwiseNO
.
๐ Your Solutions
๐น Quick and Rough Solution
๐น Better Solution (iterative check)
✅ Evaluation of Both Solutions
Criteria | Quick Solution | Better Solution |
---|---|---|
✅ Correctness | ✔️ (covers key logic) | ✔️ (covers key logic) |
✅ Simplicity | ✔️ Very concise | ✔️ Slightly more verbose |
✅ Greedy strategy (max fences) | ✔️ Replaces all dots | ✔️ Replaces all dots |
❌ Efficiency | ✔️ (linear time) | ✔️ (linear time) |
✅ Edge-case handling | ❌ No bounds check on empty string | ✔️ Slightly more robust |
✅ Readability | ✔️ Readable | ✔️ More explicit and debuggable |
๐ง Key Edge Cases
Make sure these are handled properly:
Input | Expected Output | Reason |
---|---|---|
H.H.H | YES HBH BH | No adjacent houses |
HH | NO | Adjacent houses |
. | YES B | Just fence |
H | YES H | Single house |
.H. | YES BHB | Should put fences on both sides |
H..H | YES HBBH | Most fences possible |
H.H.H.H.H | YES HBHBHBHBH | Alternating pattern |
H...H | YES HBBBH | Max fences |
All your logic (especially in the second solution) correctly accounts for these.
๐ง Suggested Optimized Version (Clean & Clear)
✅ Why this is the best form:
-
Handles adjacent
H
s robustly. -
Replaces
.
withB
(greedy placement of fences). -
Minimal and readable.
-
Good for competitive programming.
๐งช Testing
Here are a few test cases to check:
✨ Bonus Optimization (One-liner Pythonic variant)
But this sacrifices readability for brevity — not ideal in interviews or maintainable codebases.
✅ Final Verdict
Your understanding is spot-on, and both solutions are valid. The second one is better in terms of flexibility and future-proofing.
๐ Recommendations:
-
Stick with the second (iterative) approach in interviews or contests.
-
Optionally add a helper function to encapsulate logic for cleaner testing.
-
Always handle edge cases like input length 1 or strings with only dots.
Let me know if you want the same logic in C++ or Java!
Can you also do a space time complexity analysis of two solutions?
Absolutely! Let's analyze the time and space complexity of your two solutions for the Split House problem.
๐ Problem Recap
Given a string of length n
(1 ≤ n ≤ 20) consisting of characters:
-
'H'
= House -
'.'
= Empty space
You must:
-
Check if any two
H
s are adjacent (invalid). -
Replace every
'.'
with'B'
to insert fences. -
Print
YES
and the modified string, orNO
.
๐ง Quick & Rough Solution
✅ Logic Recap:
⏱ Time Complexity:
Operation | Time |
---|---|
'HH' in s | O(n) |
s.replace('.', 'B') | O(n) |
Overall | O(n) |
Since n ≤ 20
, this is constant-time in practice, but linear in terms of Big-O.
๐ง Space Complexity:
Variable | Space |
---|---|
Input string | O(n) (immutable string) |
Replaced string | O(n) (new string after replacement) |
Total | O(n) |
Note: Since strings are immutable in Python,
.replace()
creates a new string.
๐ง Better (Iterative) Solution
✅ Logic Recap:
⏱ Time Complexity:
Operation | Time |
---|---|
Loop to check 'HH' | O(n) |
Loop to replace . with B | O(n) |
''.join() | O(n) |
Overall | O(n) |
Same as before — all linear operations.
๐ง Space Complexity:
Variable | Space |
---|---|
s = list(input()) | O(n) |
No extra structures | Reusing list |
Final string | O(n) via join() |
Total | O(n) |
๐ Summary Table
Metric | Quick Solution | Iterative Solution |
---|---|---|
Time Complexity | O(n) | O(n) |
Space Complexity | O(n) | O(n) |
String Mutability | New string (replace ) | List mutated in-place |
Ease of Use | Short, elegant | More explicit and readable |
Best for Interviews | ❌ Not very transparent | ✅ Clear and step-by-step |
๐ง Which One Should You Use?
-
For interviews or code readability, the iterative version is better: it explicitly shows validation and mutation.
-
For contests or one-liners, the quick solution is fine.
Both are optimal given the input constraints (n ≤ 20
). You can't significantly optimize further unless working with much larger inputs.
Let me know if you'd like to test these with n = 20
or see them translated to another language like Java or C++!
No comments:
Post a Comment