원하는 알파벳을 완성할때
좌-우 중 어디로 움직여야하는지
위-아래 중 어디로 움직어야 최소 값을 찾을 수 있는지 파악하는 문제
solution.solution("AAAAAA");
solution.solution("JBAAAB");
solution.solution("JEROEN");
solution.solution("JAN");
final List<String> A_TO_Z = Arrays.asList(new String[]{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"});
String[] splitName;
public Queue<Map<String, Object>> queue = new LinkedList<>();
int lowestMoveCount = -1;
public int solution(String name) {
int answer = 0;
queue = new LinkedList<>();
splitName = name.split("");
lowestMoveCount = -1;
int currentLocation = 0;
boolean[] matchStatus = new boolean[splitName.length];
int moveCount = findSameAlphabet(splitName[currentLocation]);
matchStatus[currentLocation] = true;
for(int i = 0; i < splitName.length; i++){
if(splitName[i].equals("A")) matchStatus[i] = true;
}
if (allMatch(matchStatus)) {
if (moveCount < 0) answer = 0;
else answer = moveCount;
}
else{
addQueue(moveCount, currentLocation, matchStatus);
answer = findLowestMoveSameName();
}
System.out.println("lowestMoveSameName = " + answer);
return answer;
}
int findLowestMoveSameName(){
while (queue.size() > 0){
Map<String, Object> poll = queue.poll();
int moveCount = (int) poll.get("moveCount");
boolean[] matchStatus = (boolean[]) poll.get("matchStatus");
int currentLocation = (int) poll.get("currentLocation");
if(allMatch(matchStatus)) {
if(lowestMoveCount == -1){
lowestMoveCount = moveCount;
} else{
lowestMoveCount = lowestMoveCount > moveCount ? moveCount : lowestMoveCount;
}
}else{
moveLeft(moveCount, currentLocation, matchStatus.clone());
moveRight(moveCount, currentLocation, matchStatus.clone());
}
}
return lowestMoveCount;
}
void moveLeft(int moveCount, int currentLocation, boolean[] matchStatus) {
++moveCount;
if(currentLocation == 0) currentLocation = splitName.length-1;
else currentLocation--;
if(matchStatus[currentLocation]){
moveLeft(moveCount, currentLocation, matchStatus);
}else{
moveCount += findSameAlphabet(splitName[currentLocation]);
matchStatus[currentLocation] = true;
addQueue(moveCount, currentLocation, matchStatus);
}
}
void moveRight(int moveCount, int currentLocation, boolean[] matchStatus) {
++moveCount;
if(currentLocation == splitName.length-1) currentLocation = 0;
else currentLocation++;
if(matchStatus[currentLocation]){
moveRight(moveCount, currentLocation, matchStatus);
}else{
moveCount += findSameAlphabet(splitName[currentLocation]);
matchStatus[currentLocation] = true;
addQueue(moveCount, currentLocation, matchStatus);
}
}
void addQueue(int moveCount, int currentLocation, boolean[] matchStatus) {
Map<String, Object> map = new HashMap<>();
map.put("matchStatus", matchStatus);
map.put("moveCount", moveCount);
map.put("currentLocation", currentLocation);
queue.add(map);
}
boolean allMatch(boolean[] matchStatus){
boolean returnValue = true;
for(boolean match : matchStatus){
if(match == false) returnValue = false;
}
return returnValue;
}
int findSameAlphabet(String alphabet){
if("A".equals(alphabet)) return 0;
int upScroll = upScroll(alphabet);
int downScroll = downScroll(alphabet);
return upScroll >= downScroll ? downScroll : upScroll;
}
int upScroll(String alphabet){
return A_TO_Z.indexOf(alphabet);
}
int downScroll(String alphabet){
return A_TO_Z.size() - A_TO_Z.indexOf(alphabet);
}
}