Ad
Strings

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.
Code
Diff
  • 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);
            }
          }
        }
      }
      
      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.split(" ");
    • List<String> listKey = new ArrayList<String>();
    • 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);
    • }
    • }
    • }
    • }
    • 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();
    • }
    • }
Strings
Code
Diff
  • 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);
            }
          }
        }
      }
      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.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 "";
    • 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.split(" ");
List<String> listKey = new ArrayList<String>();
PriorityQueue<String> listValue = new PriorityQueue<>();
TreeMap<String, String> alphabet = new TreeMap<>();
  return "";
  }

}