CodingBat: Java. Map-2


For further help with Coding Bat (Java), please check out my books. I am also available for tutoring.


Here are my solutions to the Map-2 section on CodingBat.

word0:

public Map<String, Integer> word0(String[] strings) {
  
  Map<String, Integer> map = new HashMap<String, Integer>();
  
  for (int i = 0; i < strings.length; i++) {
    map.put(strings[i], 0);
  }
  
  return map;
}

wordLen:

public Map<String, Integer> wordLen(String[] strings) {
    
  Map<String, Integer> map = new HashMap<String, Integer>();
  
  for (int i = 0; i < strings.length; i++) {
    String tmp = strings[i];
    map.put(tmp, tmp.length());
  }
  
  return map;
}

pairs:

public Map<String, String> pairs(String[] strings) {
  
  Map<String, String> map = new HashMap<String, String>();
  
  for (int i = 0; i < strings.length; i++) {
    String tmp   = strings[i];
    String first = String.valueOf(tmp.charAt(0));
    String last  = String.valueOf(tmp.charAt(tmp.length() - 1));
    map.put(first, last);
  }
  
  return map;
}

wordCount:

public Map<String, Integer> wordCount(String[] strings) {
  
  Map<String, Integer> map = new HashMap<String, Integer>();
  
  for (int i = 0; i < strings.length; i++) {
    
    String tmp = strings[i];
    
    if (map.containsKey(tmp)) {
      int count = map.get(tmp);
      map.put(tmp, count + 1);
    } else {
      map.put(tmp, 1);
    }
    
  }
  return map;
}

firstChar:

public Map<String, String> firstChar(String[] strings) {
  
  Map<String, String> map = new HashMap<String, String>();
  
  for (int i = 0; i < strings.length; i++) {
    
    String key = String.valueOf(strings[i].charAt(0));
    
    if (map.containsKey(key)) {
      String val = map.get(key) + strings[i];
      map.put(key, val);
    } else {
      map.put(key, strings[i]);
    }
    
  }
  return map;
}

wordAppend:

public String wordAppend(String[] strings) {
 
 Map<String, Integer> map    = new HashMap<String, Integer>();
 String               result = "";
 
 for (int i = 0; i < strings.length; i++) {
   
   String key = strings[i];
   
   if (map.containsKey(key)) {
     int val = map.get(key);
     val++;
     if (val % 2 == 0) {
      result += key;
     }
     map.put(key, val);
   } else {
     map.put(key, 1);
   }
   
 }
 
 return result;
}

wordMultiple:

public Map<String, Boolean> wordMultiple(String[] strings) {
  
  Map<String, Integer> counts = new HashMap<String, Integer>();
  Map<String, Boolean> result = new HashMap<String, Boolean>();
  
  for (int i = 0; i < strings.length; i++) {
    String key = strings[i];
    
    if (counts.containsKey(key)) {
      int val = counts.get(key);
      val++;
      counts.put(key, val);
    } else {
      counts.put(key, 1);
    }
    
    result.put(key, counts.get(key) >= 2);
  }
  
  return result;
}

allSwap:

public String[] allSwap(String[] strings) {
  
  Map<String, Integer> map = new HashMap<String, Integer>();
    
  for (int i = 0; i < strings.length; i++) {
    
    String key = String.valueOf(strings[i].charAt(0));
    
    if (map.containsKey(key)) {
      
      // swap
      int    pos   = map.get(key); 
      String tmp   = strings[pos];
      strings[pos] = strings[i];
      strings[i]   = tmp ;
      
      // delete
      map.remove(key);
      
    } else {
      map.put(key, i);
    }
    
  }
  
  return strings;
}

firstSwap:

public String[] firstSwap(String[] strings) {
  
  Map<String, Integer> map = new HashMap<String, Integer>();
    
  for (int i = 0; i < strings.length; i++) {
    
    String key = String.valueOf(strings[i].charAt(0));
    
    if (map.containsKey(key)) {
      
      int val = map.get(key);
      if (val == -1) {
        continue;
      }
      
      // swap
      int    pos   = map.get(key); 
      String tmp   = strings[pos];
      strings[pos] = strings[i];
      strings[i]   = tmp ;
      
      // set a flag
      map.put(key, -1);
      
    } else {
      map.put(key, i);
    }
    
  }
  
  return strings;
}

For further help with Coding Bat (Java), please check out my books. I am also available for tutoring.


16 thoughts on “CodingBat: Java. Map-2

  1. Wazim Karim

    Just another solution to wordMultiple.
    Instead of making two HashMaps; just use one.

    public Map wordMultiple(String[] strings) {
    Map map = new HashMap();
    for (String s : strings){
    if (map.containsKey(s) == false){
    map.put(s, false);
    } else {
    map.put(s, true);
    }
    }
    return map;
    }

    Reply
  2. Wazim Karim

    public Map wordMultiple(String[] strings) {
    Map map = new HashMap();
    for (String s : strings){
    if (map.containsKey(s) == false){
    map.put(s, false);
    } else {
    map.put(s, true);
    }
    }
    return map;
    }

    Reply
    1. Gregor Ulm Post author

      Just look up how to properly insert source code in WordPress comments. You probably need a special tag for that.

      Reply
  3. tan

    public String[] firstSwap(String[] strings) {

    Map map = new HashMap();
    Map store = new HashMap();
    Map counter = new HashMap();

    for(int i = 0; i 2){
    break;
    }

    if(map.containsKey(key)){
    String temp = strings[store.get(key + map.get(key))];
    strings[store.get(key + map.get(key))] = strings[i];
    strings[i] = temp;
    map.remove(key);
    store.put(strings[i], i);
    }else{
    store.put(strings[i], i);
    map.put(key, strings[i].substring(1));
    }
    }

    return strings;
    }
    Question: How do I stop coming up with redundant solutions like this on the first attempt to the question? This been happening a lot.

    Reply
    1. tan

      public String[] firstSwap(String[] strings) {

      Map map = new HashMap();
      Map store = new HashMap();
      Map counter = new HashMap();

      for(int i = 0; i 2){
      break;
      }

      if(map.containsKey(key)){
      String temp = strings[store.get(key + map.get(key))];
      strings[store.get(key + map.get(key))] = strings[i];
      strings[i] = temp;
      map.remove(key);
      store.put(strings[i], i);
      }else{
      store.put(strings[i], i);
      map.put(key, strings[i].substring(1));
      }
      }

      return strings;
      }

      Reply
  4. Eashwar

    I came up with a slightly more concise wordCount solution:

    public Map wordCount(String[] strings) {
    Map map = new HashMap();
    for(String s:strings)
    map.put(s, Collections.frequency(Arrays.asList(strings), s));
    return map;
    }

    Reply
  5. Ken P

    After a lot of frustration and re-thinking, it turns out wordAppend doesn’t even need a map. Just one forward loop with a recurring back-looking loop:

    public String wordAppend(String[] strings) {

    String output= “”;
    int count = 0;
    for (int i = 0; i-1; j–){
    if (strings[j] == strings[i]) count++;
    }
    if (count % 2 == 0) output += strings[i];
    count = 0;
    }
    return output;

    }

    Reply
    1. Ken P

      The code got snipped. Forgot to use tags. Here it is properly:

      public String wordAppend(String[] strings) {

      String output= "";
      int count = 0;
      for (int i = 0; i-1; j--){
      if (strings[j] == strings[i]) count++;
      }
      if (count % 2 == 0) output += strings[i];
      count = 0;
      }
      return output;

      }

      Reply
      1. Ken P

        Not sure if I mis-pasted the code or it was snipped again?

        public String wordAppend(String[] strings) {
        String output= "";
        int count = 0;
        for (int i = 0; i-1; j--){
        if (strings[j] == strings[i]) count++;
        }
        if (count % 2 == 0) output += strings[i];
        count = 0;
        }
        return output;
        }

        Reply
  6. Generic

    Another solution to firstSwap using Map and ArrayList.

    public String[] firstSwap(String[] strings) {

    Map proMap = new HashMap();
    List proList = new ArrayList();

    for(int i=0; i<strings.length; i++){

    String key = String.valueOf(strings[i].charAt(0));

    if(proMap.containsKey(key) && !(proList.contains(key))){

    int pos = proMap.get(key);
    String tmp = strings[pos];
    strings[pos] = strings[i];
    strings[i] = tmp;
    proList.add(key);
    } else{
    proMap.put(key, i);
    }
    }
    return strings;
    }

    Reply
  7. Chris

    wordLen:

    public Map wordLen(String[] strings) {
    return Arrays.stream(strings)
    .collect(Collectors.toMap(s -> {
    return s;
    }, String::length, (a, b) -> b));

    }

    Reply
    1. Gregor Ulm Post author

      Try doing it without those inbuilt functions! You’re cheating yourself out of learning basic programming skills. Regardless of what you may think, you will not be able to skip that step.

      Reply
  8. Chris

    pairs:
    public Map pairs(String[] strings) {
    return Arrays.stream(strings)
    .collect(Collectors
    .toMap(s -> {
    return String.valueOf(s.charAt(0));
    }, s -> String.valueOf(s.charAt(s.length() – 1)), (a, b) -> b));
    }

    Reply
  9. Chris

    wordAppend:

    public String wordAppend(String[] strings) {

    Map map = new HashMap();
    String result = “”;

    for (String key : strings) {

    if (map.containsKey(key)) {
    int val = map.get(key);
    val++;
    if (val % 2 == 0) {
    result += key;
    }
    map.put(key, val);
    } else {
    map.put(key, 1);
    }

    }

    return result;
    }

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Spammer prevention; the answer is an integer: * Time limit is exhausted. Please reload CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.