Map-2

word0

my solution

public Map<String, Integer> word0(String[] strings) {
  Map<String, Integer> map = new HashMap<>();
  for (String s : strings) {
    if (map.containsKey(s)) continue;
    map.put(s, 0);
  }
  return map;
}

wordLen

  • 딱히 if 문 처리로 존재하는지 안하는지 업데이트 필요 없는 경우

public Map<String, Integer> wordLen(String[] strings) {
  Map<String, Integer> map = new HashMap();
  for (String s : strings) {
    map.put(s, s.length());
  }
  return map;
}

pairs

public Map<String, String> pairs(String[] strings) {
  Map<String, String> map = new HashMap();
  for (String s : strings) {
    map.put(s.charAt(0) +"", s.charAt(s.length() -1) +"");
  }
  return map;
}
public Map<String, String> pairs(String[] strings) {
    Map<String, String> map = new HashMap();
    for (String s : strings) {
        map.put(s.substring(0, 1), s.substring(s.length() - 1, s.length()));
    }
    return map;
}

wordCount

public Map<String, Integer> wordCount(String[] strings) {
  Map<String, Integer> map = new HashMap();
  for (String s : strings) {
    if (map.containsKey(s)) map.put(s, map.get(s) + 1);
    else map.put(s, 1);
  }
  return map;
}

firstChar

public Map<String, String> firstChar(String[] strings) {
  Map<String, String> map = new HashMap();
  for (String s : strings) {
    String firstKey = s.charAt(0) +"";
    if (map.containsKey(firstKey))
      map.put(firstKey, map.get(firstKey) + s);
    else 
      map.put(firstKey, s);
  }
  return map;
}

wordAppend

public String wordAppend(String[] strings) {
  Map<String, Integer> map = new HashMap();
  String answer = "";
  
  for (String s : strings) {
    if(map.containsKey(s)) {
      if (map.get(s) == 1) {
        answer += s;
        map.put(s, 0);
      }
      else map.put(s, map.get(s) + 1);
    }
    else {
      map.put(s, 1);
    }
  }
  
  return answer;
}

wordMultiple

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

allSwap

Question

We'll say that 2 strings "match" if they are non-empty and their first chars are the same. Loop over and then return the given array of non-empty strings as follows: if a string matches an earlier string in the array, swap the 2 strings in the array. When a position in the array has been swapped, it no longer matches anything. Using a map, this can be solved making just one pass over the array. More difficult than it looks.

allSwap(["ab", "ac"]) → ["ac", "ab"]

allSwap(["ax", "bx", "cx", "cy", "by", "ay", "aaa", "azz"]) → ["ay", "by", "cy", "cx", "bx", "ax", "azz", "aaa"]

allSwap(["ax", "bx", "ay", "by", "ai", "aj", "bx", "by"]) → ["ay", "by", "ax", "bx", "aj", "ai", "by", "bx"]

public String[] allSwap(String[] strings) {
  Map<String, Integer> map = new HashMap();
  String[] str = new String[strings.length];
  
  for (int i = 0; i < strings.length; i++) {
    if (map.containsKey(strings[i].charAt(0) + "")) {
      // chagne
      str[map.get(strings[i].charAt(0) + "")] = strings[i];
      str[i] = strings[map.get(strings[i].charAt(0) + "")];
      // and
      // remove
      map.remove(strings[i].charAt(0) + "");
    }
    else { 
      map.put(strings[i].charAt(0) + "", i);
      str[i] = strings[i];
    }
  }
  
  return str;
}

firstSwap

Question

We'll say that 2 strings "match" if they are non-empty and their first chars are the same. Loop over and then return the given array of non-empty strings as follows: if a string matches an earlier string in the array, swap the 2 strings in the array. A particular first char can only cause 1 swap, so once a char has caused a swap, its later swaps are disabled. Using a map, this can be solved making just one pass over the array. More difficult than it looks.

firstSwap(["ab", "ac"]) → ["ac", "ab"]

firstSwap(["ax", "bx", "cx", "cy", "by", "ay", "aaa", "azz"]) → ["ay", "by", "cy", "cx", "bx", "ax", "aaa", "azz"]

firstSwap(["ax", "bx", "ay", "by", "ai", "aj", "bx", "by"]) → ["ay", "by", "ax", "bx", "ai", "aj", "bx", "by"]

public String[] firstSwap(String[] strings) {
  Map<String, Integer> map = new HashMap();
  Map<String, Boolean> checkFirst = new HashMap();
  String[] str = new String[strings.length];
  
  for (int i = 0; i < strings.length; i++) {
    if (map.containsKey(strings[i].substring(0, 1))) {

      if (checkFirst.get(strings[i].substring(0, 1))) {
        str[map.get(strings[i].substring(0, 1))] = strings[i];
        str[i] = strings[map.get(strings[i].substring(0, 1))];
        checkFirst.put(strings[i].substring(0, 1), false);
      } else {
        str[i] = strings[i];
      }

    } else {
      map.put(strings[i].substring(0, 1), i);
      str[i] = strings[i];
      checkFirst.put(strings[i].substring(0, 1), true);
    }
  }
  
  return str;
}
  • 위의 코드를 리팩토링 하였다.

public String[] firstSwap(String[] strings) {
  Map<String, Integer> map = new HashMap();
  Map<String, Boolean> checkFirst = new HashMap();
  String[] str = new String[strings.length];
  
  for (int i = 0; i < strings.length; i++) {
    String firstKey = strings[i].substring(0, 1);
    if (map.containsKey(firstKey)) {

      if (checkFirst.get(firstKey)) {
        str[map.get(firstKey)] = strings[i];
        str[i] = strings[map.get(firstKey)];
        checkFirst.put(firstKey, false);
      } else {
        str[i] = strings[i];
      }
      
    } else {
      map.put(firstKey, i);
      str[i] = strings[i];
      checkFirst.put(firstKey, true);
    }
  }
  
  return str;
}

Last updated