## The challenge#

Given a string and an array of integers representing indices, capitalize all letters at the given indices.

Example:

• capitalize("abcdef",[1,2,5]) = "aBCdeF"
• capitalize("abcdef",[1,2,5,100]) = "aBCdeF". There is no index 100.

The input will be a lowercase string with no spaces and an array of digits.

## The solution in Java code#

Option 1:

package solution;

import java.util.*;

public class Solution{
public static String capitalize(String s, int[] ind){
char[] chars = s.toCharArray();
Arrays.stream(ind)
.filter(c -> c < s.length())
.forEach(c -> chars[c] = Character.toUpperCase(chars[c]));
return new String(chars);
}
}

Option 2:

package solution;

public class Solution {
public static String capitalize(String s, int[] ind){
char[] result = s.toCharArray();

for(int i : ind) {
if(i < result.length) result[i] = Character.toUpperCase(result[i]);
else continue;
}
String str = String.valueOf(result);
return str;

}
}

Option 3:

package solution;

public interface Solution {
static String capitalize(String s, int[] ind) {
for (int i : ind) {
if (i < s.length()) {
s = s.substring(0, i) + (char) (s.charAt(i) - 32) + s.substring(i + 1);
}
}
return s;
}
}

## Test cases to validate our solution#

import java.util.Random;
import java.util.TreeSet;

import static Solution.capitalize;

import org.junit.runners.JUnit4;
import org.junit.Test;
import static org.junit.Assert.assertEquals;

public class SolutionTest{
private static final String lowercase = "abcdefghijklmnopqrstuvwxyz";
private static final Random random = new Random();

@Test
public void basicTest(){
assertEquals("aBCdeF", capitalize("abcdef", new int[]{1,2,5}));
assertEquals("aBCdeF", capitalize("abcdef", new int[]{1,2,5,100}));
assertEquals("Indexinglessons", capitalize("indexinglessons", new int[]{0}));
}

private String solve_kvWOF(String s, int[] ind){
StringBuilder sb = new StringBuilder(s);
for(int i : ind){
if(i < sb.length())
sb.setCharAt(i, Character.toUpperCase(sb.charAt(i)));
}
return sb.toString();
}

@Test
public void randomTest(){
for(int i = 0; i < 100; i++){
int r = random.nextInt(10) + 10;
StringBuilder sb = new StringBuilder();
for(int j = 0; j < r; j++)
sb.append(lowercase.charAt(random.nextInt(lowercase.length())));
TreeSet<Integer> set = new TreeSet<Integer>();
for(int j = 0; j < sb.length(); j++)