Indexed Capitalization in Java


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("abRacaDabRA", capitalize("abracadabra", new int[]{2,6,9,10}));
        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++)
                set.add(random.nextInt(r));
            int[] a = new int[set.size()];
            for(int j = 0; j < a.length; j++)
                a[j] = set.pollFirst();
            String str = sb.toString();
            assertEquals(solve_kvWOF(str, a), capitalize(str, a));
        }
    }
}