You are supposed to write production ready code with Proper Unit Test Cases, Modularizaton etc. You need to write your test cases under user package in test folder. One sample test is created under same package for reference. Your code will be evaluated on the basis of code coverage, Code Quality, Best Practices and Bugs. NOTE: Here, we use JAVA 8 and GRADLE VERSION 4+. PROBLEM: You are given a string with Alphabet set B,D,U and H. This string will be considered steady if each letter occurs only n/4 times. For Example HBDU and BBHUHDDU both are steady strings. Your job is to make a string steady by replacing substring of input string with any string of the same length. It can be a empty string also. I Return the length of the minimum length substring that can be replaced to make string stable. CONSTRAINTS: For input character array 1<= size of array <=100. FUNCTIONAL DESCRIPTION: Provide implementation for makeStringSteady(String input). EXAMPLE: Input: HBBBUBBB Output: 5 Explanation: One optimal solution is to replace BBBUB with UUDDH resulting in HUUDDHBB

Aman Patel
3 min readDec 15, 2023

You are supposed to write production ready code with Proper Unit Test Cases, Modularizaton etc. You need to write your test cases under user package in test folder. One sample test is created under same package for reference. Your code will be evaluated on the basis of code coverage, Code Quality, Best Practices and Bugs. NOTE: Here, we use JAVA 8 and GRADLE VERSION 4+. PROBLEM: You are given a string with Alphabet set B,D,U and H. This string will be considered steady if each letter occurs only n/4 times. For Example HBDU and BBHUHDDU both are steady strings. Your job is to make a string steady by replacing substring of input string with any string of the same length. It can be a empty string also. I Return the length of the minimum length substring that can be replaced to make string stable. CONSTRAINTS: For input character array 1<= size of array <=100. FUNCTIONAL DESCRIPTION: Provide implementation for makeStringSteady(String input). EXAMPLE: Input: HBBBUBBB Output: 5 Explanation: One optimal solution is to replace BBBUB with UUDDH resulting in HUUDDHBB

// src/main/java/user/StringSteady.java

// src/main/java/user/StringSteady.java

package user;

public class StringSteady {

public static int makeStringSteady(String input) {
int n = input.length();
int targetCount = n / 4;

int[] count = new int[4];
for (char c : input.toCharArray()) {
switch (c) {
case 'B':
count[0]++;
break;
case 'D':
count[1]++;
break;
case 'U':
count[2]++;
break;
case 'H':
count[3]++;
break;
}
}

int minLength = Integer.MAX_VALUE;
int start = 0;
for (int end = 0; end < n; end++) {
count[input.charAt(end) - 'B']--;

while (start < n && count[0] <= targetCount && count[1] <= targetCount
&& count[2] <= targetCount && count[3] <= targetCount) {
minLength = Math.min(minLength, end - start + 1);
count[input.charAt(start) - 'B']++;
start++;
}
}

return minLength;
}
}

// src/test/java/user/StringSteadyTest.java

// src/main/java/user/StringSteady.java

package user;

public class StringSteady {

public static int makeStringSteady(String input) {
int n = input.length();
int targetCount = n / 4;

int[] count = new int[4];
for (char c : input.toCharArray()) {
switch (c) {
case 'B':
count[0]++;
break;
case 'D':
count[1]++;
break;
case 'U':
count[2]++;
break;
case 'H':
count[3]++;
break;
}
}

int minLength = Integer.MAX_VALUE;
int start = 0;
for (int end = 0; end < n; end++) {
count[input.charAt(end) - 'B']--;

while (start < n && count[0] <= targetCount && count[1] <= targetCount
&& count[2] <= targetCount && count[3] <= targetCount) {
minLength = Math.min(minLength, end - start + 1);
count[input.charAt(start) - 'B']++;
start++;
}
}

return minLength;
}
}

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Aman Patel
Aman Patel

Written by Aman Patel

0 Followers

Full Stack Developer

Responses (2)

Write a response

Below is the correct solution using map in c++
class Solution {
public:
bool isBalanced(unordered_map<char,int>&ump,int balancedSize)
{
for(auto x : ump)
{
if(x.second > balancedSize)
return false;
}
return true;
}
int balancedString(string s) {
int balancedSize…

Hi Aman,
looks like you are having some issues in your code.
for the test case "HBBBUBBB"
count[input.charAt(end) - 'B']--
It will give you NullPointerException as the index value will be as 6 when char is 'H' but the count will have only 4 size. Please have a look try to correct.