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
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;
}
}