import java.util.*; public class Kata { public static String keyAlphabet(String str) { String[] arr = str.toLowerCase().split(" "); HashSet<String> listKey = new HashSet<>(); PriorityQueue<String> listValue = new PriorityQueue<>(); TreeMap<String, String> alphabet = new TreeMap<>(); for (String item : arr) { if (item.length() == 1 && item.charAt(0) >= 'a' && item.charAt(0) <= 'z') { listKey.add(item); } else { listValue.add(item); } } ArrayList<String> values = new ArrayList<>(listValue); for (String value : values) { for (String s : listKey) { if (value.contains(s)) { if (alphabet.containsKey(s)) { alphabet.put(s, alphabet.get(s) + "," + value); } else { alphabet.put(s, value); } } } } StringBuilder result = new StringBuilder(); for (Map.Entry<String, String> m : alphabet.entrySet()) { result.append(m.getKey()).append(":").append(m.getValue()).append(" "); } return result.toString().trim(); } }
- import java.util.*;
- public class Kata {
- public static String keyAlphabet(String str) {
String[] arr = str.toLowerCase().split(" ");TreeSet<String> listKey = new TreeSet<String>();PriorityQueue<String> listValue = new PriorityQueue<>();TreeMap<String, String> alphabet = new TreeMap<>();for (String item : arr) {if (item.length() == 1 && item.charAt(0) >= 'a' && item.charAt(0) <= 'z') {listKey.add(item);} else {listValue.add(item);}}List<String> values = new ArrayList<>(listValue);for (String value : values) {for (String s : listKey) {if (value.contains(s)) {if (alphabet.containsKey(s)) {alphabet.put(s, alphabet.get(s) + "," + value);} else {alphabet.put(s, value);- String[] arr = str.toLowerCase().split(" ");
- HashSet<String> listKey = new HashSet<>();
- PriorityQueue<String> listValue = new PriorityQueue<>();
- TreeMap<String, String> alphabet = new TreeMap<>();
- for (String item : arr) {
- if (item.length() == 1 && item.charAt(0) >= 'a' && item.charAt(0) <= 'z') {
- listKey.add(item);
- } else {
- listValue.add(item);
- }
- }
}}}StringBuilder result = new StringBuilder();for (Map.Entry<String, String> m : alphabet.entrySet()) {result.append(m.getKey()).append(":").append(m.getValue()).append(" ");}return result.toString().trim();- ArrayList<String> values = new ArrayList<>(listValue);
- for (String value : values) {
- for (String s : listKey) {
- if (value.contains(s)) {
- if (alphabet.containsKey(s)) {
- alphabet.put(s, alphabet.get(s) + "," + value);
- } else {
- alphabet.put(s, value);
- }
- }
- }
- }
- StringBuilder result = new StringBuilder();
- for (Map.Entry<String, String> m : alphabet.entrySet()) {
- result.append(m.getKey()).append(":").append(m.getValue()).append(" ");
- }
- return result.toString().trim();
- }
- }
import java.util.*; import org.junit.Test; import static org.junit.Assert.assertEquals; import org.junit.runners.JUnit4; public class SolutionTest { @Test public void sampleTests() { //Sample tests assertEquals("a:11a1 b:222b22 c:3c33", Kata.keyAlphabet("a b c 11a1 222b22 3c33")); assertEquals("a:11a1,222a33 b:222b22 c:3c33", Kata.keyAlphabet("222a33 a b c 11a1 222b22 3c33 5")); assertEquals("a:11a1,12a1,13a1 c:33c3 d:2d2", Kata.keyAlphabet("a d c x 11a1 2d2 33c3 13a1 12a1")); assertEquals("", Kata.keyAlphabet("")); assertEquals("a:11a1 b:2b2 c:33c3", Kata.keyAlphabet("a b c d 11a1 33c3 2b2")); assertEquals("a:11a1 b:2b2 c:33c3", Kata.keyAlphabet("a b c 11a1 33c3 2b2 d44")); //Tests assertEquals("a:a90,a90 b:12b6,33b4", Kata.keyAlphabet("a b c d e f g h i j k l m n o p q r s t u v w y z 33b4 5x5 12b6 A90 A90")); assertEquals("a:a11", Kata.keyAlphabet("a a a11")); } }
- import java.util.*;
- import org.junit.Test;
- import static org.junit.Assert.assertEquals;
- import org.junit.runners.JUnit4;
- public class SolutionTest {
- @Test
- public void sampleTests() {
- //Sample tests
- assertEquals("a:11a1 b:222b22 c:3c33", Kata.keyAlphabet("a b c 11a1 222b22 3c33"));
- assertEquals("a:11a1,222a33 b:222b22 c:3c33", Kata.keyAlphabet("222a33 a b c 11a1 222b22 3c33 5"));
- assertEquals("a:11a1,12a1,13a1 c:33c3 d:2d2", Kata.keyAlphabet("a d c x 11a1 2d2 33c3 13a1 12a1"));
- assertEquals("", Kata.keyAlphabet(""));
- assertEquals("a:11a1 b:2b2 c:33c3", Kata.keyAlphabet("a b c d 11a1 33c3 2b2"));
- assertEquals("a:11a1 b:2b2 c:33c3", Kata.keyAlphabet("a b c 11a1 33c3 2b2 d44"));
- //Tests
- assertEquals("a:a90,a90 b:12b6,33b4", Kata.keyAlphabet("a b c d e f g h i j k l m n o p q r s t u v w y z 33b4 5x5 12b6 A90 A90"));
- assertEquals("a:a11", Kata.keyAlphabet("a a a11"));
/*String SALTCHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";StringBuilder salt = new StringBuilder();Random rnd = new Random();while (salt.length() < 18) {int index = (int) (rnd.nextFloat() * SALTCHARS.length());salt.append(SALTCHARS.charAt(index));}String saltStr = salt.toString();*/- }
- }
INTRODUCTION
Welcome soldiers, we need your help! The enemy army
is going to attack and we have to get ready.
Get together quickly by battalions!
TASK
Your task as a colonel is to order your lieutenants
with their respective battalion, for each lieutenant
a letter is assigned and each battalion is formed by
numbers and the letter corresponding to its lieutenant.
Your duty is to assign them in such a way that you
return a string like the following:
"a:11a1 b:b22 c:33c".
EXAMPLES
keyAlphabet( "a b c 11a b22 c33" ) => returns "a:11a b:b22 c:c33"
keyAlphabet( "a b c 11a1 2b2 33c3 13a1 12a1" ) => returns "a:11a1,12a1,13a1 b:2b2 c:33c3"
NOTES
- There may be more than one battalion for the same lieutenant.
- Both lieutenants and battalions must be ordered.
- Be careful with the spaces.
import java.util.*; public class Kata { public static String keyAlphabet(String str) { String[] arr = str.split(" "); List<String> listKey = new ArrayList<String>(); PriorityQueue<String> listValue = new PriorityQueue<>(); TreeMap<String, String> alphabet = new TreeMap<>(); for (String item : arr) { if (item.length() == 1 && item.charAt(0) >= 'a' && item.charAt(0) <= 'z') { listKey.add(item); } else { listValue.add(item); } } List<String> values = new ArrayList<>(listValue); for (String value : values) { for (String s : listKey) { if (value.contains(s)) { if (alphabet.containsKey(s)) { alphabet.put(s, alphabet.get(s) + "," + value); } else { alphabet.put(s, value); } } } } return ""; } }
- import java.util.*;
- public class Kata {
- public static String keyAlphabet(String str) {
- String[] arr = str.split(" ");
- List<String> listKey = new ArrayList<String>();
- PriorityQueue<String> listValue = new PriorityQueue<>();
- TreeMap<String, String> alphabet = new TreeMap<>();
- for (String item : arr) {
- if (item.length() == 1 && item.charAt(0) >= 'a' && item.charAt(0) <= 'z') {
- listKey.add(item);
- } else {
- listValue.add(item);
- }
- }
- List<String> values = new ArrayList<>(listValue);
- for (String value : values) {
- for (String s : listKey) {
- if (value.contains(s)) {
- if (alphabet.containsKey(s)) {
- alphabet.put(s, alphabet.get(s) + "," + value);
- } else {
- alphabet.put(s, value);
- }
- }
- }
- }
- return "";
- }
- }
prueba
import java.util.*; public class Kata { public static String keyAlphabet(String str) { String[] arr = str.split(" "); List<String> listKey = new ArrayList<String>(); PriorityQueue<String> listValue = new PriorityQueue<>(); TreeMap<String, String> alphabet = new TreeMap<>(); for (String item : arr) { if (item.length() == 1 && item.charAt(0) >= 'a' && item.charAt(0) <= 'z') { listKey.add(item); } else { listValue.add(item); } } List<String> values = new ArrayList<>(listValue); return ""; } }
- import java.util.*;
- public class Kata {
- public static String keyAlphabet(String str) {
- String[] arr = str.split(" ");
- List<String> listKey = new ArrayList<String>();
- PriorityQueue<String> listValue = new PriorityQueue<>();
- TreeMap<String, String> alphabet = new TreeMap<>();
- for (String item : arr) {
- if (item.length() == 1 && item.charAt(0) >= 'a' && item.charAt(0) <= 'z') {
- listKey.add(item);
- } else {
- listValue.add(item);
- }
- }
- List<String> values = new ArrayList<>(listValue);
- return "";
- }
- }