Problem 1:
A string consists of only ‘a’ and/or ‘b’. No three consecutive letters of one kind are allowed. Determine the minimum number of swaps between the two letters to make the string conform.
Test cases that must pass:
Aabaaab => 1
Abab => 0
Solution 1:
class Solution {
public int solution(String input) {
StringBuilder S = new StringBuilder(input);
int count = 0;
int i = 1;
while ( i < S.length())
{
if (S.charAt(i-1) != S.charAt(i)) {
i++;
continue;
}
else {
if (i + 1 < S.length() && i + 2 < S.length()) {
if (S.charAt(i+1) == S.charAt(i+2) &&
S.charAt(i+1) == S.charAt(i)){
if (S.charAt(i) == S.charAt(i-1)) {
if (S.charAt(i+2) == 'a') S.setCharAt(i+2, 'b');
else S.setCharAt(i+2, 'a');
count++;
i = i + 3;
continue;
}
else {
if (S.charAt(i+1) == 'a') S.setCharAt(i+1, 'b');
else S.setCharAt(i+1, 'a');
i = i + 2;
count++;
continue;
}
}
else if (S.charAt(i+1) == S.charAt(i)) {
if (S.charAt(i) == 'a') S.setCharAt(i, 'b');
else S.setCharAt(i, 'a');
count++;
i = i + 1;
continue;
}
else {
i = i + 1;
continue;
}
}
else if (i + 1 < S.length()) {
if (S.charAt(i+1) == s.charAt(i)) {
if (S.charAt(i) == 'a') S.setCharAt(i, 'b');
else S.setCharAt(i, 'a');
count++;
i = i + 1;
continue;
}
else {
i = i + 1;
continue;
}
}
else {
i++;
continue;
}
}
}
return count;
}
}
Problem 2:
A string consists of only ‘a’ and/or ‘b’. An integer array consists of costs of removing the letter at a given position. Determine the minimal cost to convert the string so that there are no two consecutive letters identical.
Test cases that must pass
“ab”, [1,4] => 0
“aabb”, [1,2,1,2] => 2 letters at index = 0,2 removed
“aaaa”, [3,4,5,6] => 12 letters at index = 0,1,2 removed.
Import java.lang.math;
class Solution {
public int solution(String S, int[] C) {
int cost = 0;
for (int i = 1; i < S.length(); i++)
{
if (S.charAt(i-1) == S.charAt(i)) {
cost += Math.min(C[i-1], C[i]);
}
}
return cost;
}
Problem 3:
A set of test names and their results are provided as two string arrays with the testname at index i corresponding to the result at same index. The testnames are all lowercase and conform to a prefix followed by a group number, followed by the test case specific unique lowercase alphabet suffix if there are more than one tests in that group. Results can be one of four different strings. A group that has all the tests in it with result as “OK” is valid. The tests and the results must be scored by determining the percentage of valid to total tests and returning it as a lower bound integer.
TestCase
“test1a”, “OK”
“test1b”, “OK”
“test2”, “OK”
“test3”, “Error”
“test4a”, “Timeout”
“test4b”, “OK”
“test5a”, “Error”
“test5b”, “Timeout”
“test6”, “Incorrect”
Score:33
Solution:
Import java.lang.math;
class Solution {
public int solution(String[] T, int[] R) {
int score = 0;
Map<Integer, List<String>> groupMap = new HashMap<Integer, List<String>>();
for (int I =0; I < T.length; i++)
{
// parse
StringBuilder sb = new StringBuilder();
for (int j = 0; j < T[i].length(); j++)
{
If(T[i].charAt(j) >= ‘a’ && T[i].charAt(j) <= ‘z’) {
continue;
}
sb.append(T[i].charAt(j));
}
Int group = 0;
try {
group = Integer.parseInt(sb.toString());
} catch (NumberFormatException) {
}
If (groupMap.containsKey(group)) {
groupMap.get(group).add(R[i]);
} else {
List<String> results = new ArrayList<String>();
results.add(R[i]);
groupMap.put(group, results);
}
// eval
int valid = 0;
int total = 0;
for (Map.entry<Integer, List<String>> e : groupMap.entrySet()) {
boolean passed = true;
for (String result : e.getValue()) {
if (!result.equals(“OK”)) {
passed = false;
break;
}
}
if (passed) {
valid++;
}
total++;
}
// score
score = (int)Math.floor((valid*100)/total);
}
return score;
}
No comments:
Post a Comment