How to Loop and Combine Three Strings in Java

1 min read 326 words

The challenge

Create a function that will return a string that combines all of the letters of the three inputed strings in groups. Taking the first letter of all of the inputs and grouping them next to each other. Do this for every letter, see example below!

E.g. Input: “aa”, “bb” , “cc” => Output: “abcabc”

Note: You can expect all of the inputs to be the same length.

The solution in Java code

Option 1:

public class Solution {
  public static String tripleTrouble(String one, String two, String three) {
    StringBuilder sb = new StringBuilder();
    
    for (int i=0; i<one.length(); i++) {
      sb.append(""+one.charAt(i)+two.charAt(i)+three.charAt(i));
    }
    
    return sb.toString();
  }
}

Option 2 (using IntStream):

import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class Solution {
    public static String tripleTrouble(String one, String two, String three) {
        return IntStream.range(0, one.length())
           .mapToObj(i -> "" + one.charAt(i) + two.charAt(i) + three.charAt(i))
           .collect(Collectors.joining());
    }
}

Test cases to validate our Java solution

import org.junit.Test;
import static org.junit.Assert.assertEquals;
import java.util.Random;

public class TripleTests {
  private String alph = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  private Random randGen = new Random();

  @Test
  public void basicTests() {
    System.out.println("Hard Coded Tests");
    assertEquals("abcabcabc", Solution.tripleTrouble("aaa", "bbb", "ccc"));
    assertEquals("abcabcabcabcabcabc", Solution.tripleTrouble("aaaaaa","bbbbbb","cccccc"));
    assertEquals("brrueordlnsl", Solution.tripleTrouble("burn", "reds", "roll"));
    assertEquals("Supermans", Solution.tripleTrouble("Sea", "urn", "pms"));
    assertEquals("LexLuthor", Solution.tripleTrouble("LLh","euo","xtr"));
  }
  
  @Test
  public void randomTests() {
    System.out.println("Testing 100 random inputs...");
    for (int i = 0; i < 100; i++) {
      int rand = randGen.nextInt(100);
      String one = randStr(rand);
      String two = randStr(rand);
      String three = randStr(rand);
      String msg = "Should work for:\n" + one + ",\n" + two + ",\n" + three + ".\n";
      assertEquals(msg, tripleAns(one, two, three), Solution.tripleTrouble(one, two, three));
    }
  }
  
  private String randStr(int n) {
    String t = "";
    for (int i = 0; i < n; i++) {
      t += "" + alph.charAt(randGen.nextInt(alph.length()));
    }
    return t;
  }
  
  private String tripleAns(String a, String b, String c) {
    String t = "";
    for (int i = 0; i < a.length(); i++) {
      t += a.substring(i, i+1) + b.substring(i, i+1) + c.substring(i, i+1);
    }
    return t;
  }
}
Tags:
Andrew
Andrew

Andrew is a visionary software engineer and DevOps expert with a proven track record of delivering cutting-edge solutions that drive innovation at Ataiva.com. As a leader on numerous high-profile projects, Andrew brings his exceptional technical expertise and collaborative leadership skills to the table, fostering a culture of agility and excellence within the team. With a passion for architecting scalable systems, automating workflows, and empowering teams, Andrew is a sought-after authority in the field of software development and DevOps.

Tags

Recent Posts