Convert String to LeetSpeak in Java


The challenge

Your task is to write a function toLeetSpeak that converts a regular english sentence to Leetspeak.

More about LeetSpeak You can read at wiki -> https://en.wikipedia.org/wiki/Leet

Consider only uppercase letters (no lowercase letters, no numbers) and spaces.

For example:

toLeetSpeak("LEET") returns "1337"

In this challenge we use a simple LeetSpeak dialect. Use this alphabet:

{
  A : '@',
  B : '8',
  C : '(',
  D : 'D',
  E : '3',
  F : 'F',
  G : '6',
  H : '#',
  I : '!',
  J : 'J',
  K : 'K',
  L : '1',
  M : 'M',
  N : 'N',
  O : '0',
  P : 'P',
  Q : 'Q',
  R : 'R',
  S : '$',
  T : '7',
  U : 'U',
  V : 'V',
  W : 'W',
  X : 'X',
  Y : 'Y',
  Z : '2'
}

The solution in Java code

Option 1:

import java.util.*;
class Solution {
    static String toLeetSpeak(final String speak) {
        StringBuilder sb = new StringBuilder();
        Map<Character, Character> m = new HashMap<>()
        {{
          put('A', '@');
          put('B', '8');
          put('C', '(');
          put('D', 'D');
          put('E', '3');
          put('F', 'F');
          put('G', '6');
          put('H', '#');
          put('I', '!');
          put('J', 'J');
          put('K', 'K');
          put('L', '1');
          put('M', 'M');
          put('N', 'N');
          put('O', '0');
          put('P', 'P');
          put('Q', 'Q');
          put('R', 'R');
          put('S', '$');
          put('T', '7');
          put('U', 'U');
          put('V', 'V');
          put('W', 'W');
          put('X', 'X');
          put('Y', 'Y');
          put('Z', '2');
        }};
        for (Character c : speak.toCharArray()) {
            Character a = m.getOrDefault(c, c);
            sb.append(a);
        }
        return sb.toString();
    }
}

Option 2:

class Solution {
    static String toLeetSpeak(final String speak) {
        StringBuilder sb = new StringBuilder(speak.length());
        for (char c: speak.toCharArray()) {
          switch (c) {
            case 'A': sb.append('@'); break;
            case 'B': sb.append('8'); break;
            case 'C': sb.append('('); break;
            case 'E': sb.append('3'); break;
            case 'G': sb.append('6'); break;
            case 'H': sb.append('#'); break;
            case 'I': sb.append('!'); break;
            case 'L': sb.append('1'); break;
            case 'O': sb.append('0'); break;
            case 'S': sb.append('$'); break;
            case 'T': sb.append('7'); break;
            case 'Z': sb.append('2'); break;
            default: sb.append(c); break;
          }
        }
        return sb.toString();
    }
}

Option 3:

class Solution {
  private static final String LEET = "@8(D3F6#!JK1MN0PQR$7UVWXY2";
  static String toLeetSpeak(final String speak) {
    String leet = "";
    for (final char c : speak.toCharArray())
        leet += c>='A' && c<='Z' ? LEET.charAt(c-'A') : c;
    return leet;
  }
}

Test cases to validate our solution

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

public class SolutionTest {
    @Test public void testSomething() {
        assertEquals("1337", Solution.toLeetSpeak("LEET"));
        assertEquals("#3110 W0R1D", Solution.toLeetSpeak("HELLO WORLD"));
        assertEquals("10R3M !P$UM D010R $!7 @M37", Solution.toLeetSpeak("LOREM IPSUM DOLOR SIT AMET"));
        assertEquals("7#3 QU!(K 8R0WN F0X JUMP$ 0V3R 7#3 1@2Y D06", Solution.toLeetSpeak("THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG"));
    }
}