프로그래머스. 브라이언의 고민🟡

gisung2215·2021년 1월 2일
5

👍 알고리즘

목록 보기
19/29
post-thumbnail

오늘 하루 종일 풀었는데도 못 풀었다. 브라이언이 고민할만한 것 같다. 구글링해도 관련 글이 별로 없다.😭😭

✔문제링크

프로그래머스. 브라이언의 고민

📝문제설명


위 조건을 만족하는 광고글을 다시 원래문구로 복원해야 한다.

💡해결방법

1. 문장에서 앞에서부터 단어를 하나씩 뽑아낸다.

while(sentence.length() > 0) {

	// 규칙2의 경우
	if(checkLetter(sentence, 0)) {
		...
        	words.add(sentence.substring(0, eIdx+1));
		sentence = sentence.substring(eIdx+1);
	}else {
	// 규칙 1의 경우
   	 	...
		words.add(sentence.substring(0, eIdx+2));
		sentence = sentence.substring(eIdx+2);
	}
}

2. 단어의 규칙을 찾는다.

  • 규칙1: HAHAH와 같이 단어 사이에 소문자가 들어있는 경우
  • 규칙2: aBBBBBBa 와 같이 단어 앞뒤로 소문자가 둘러싼 경우

checkLetter() 메서드를 통해 문자열의 첫글자를 확인하고, 첫글자가 소문자라면 규칙2로 아니라면 규칙1로 보고 문자열 확인을 진행했다.

// 규칙 2
if(checkLetter(sentence, 0)) { ... }
// 규칙 1
else{ ... }

3. 첫번째로 등장한 소문자와 다른 소문자를 찾는다.

다른 소문자가 등장했다는 것은 첫번째 단어가 끝났다는 뜻이다.

// 규칙2의 경우
int eIdx = 0; 
for(int i=1; i<sentence.length(); i++) {
	if(sentence.charAt(i) == ch) {
		eIdx = i;
		break;
	}
}

규칙1의 경우, 다른 소문자가 등장하는 할때

  • HaHaHoXXXo 와 같이 규칙1규칙2가 차례로 나오는 경우
  • HaHaHXoXoX와 같이 규칙1이 2번 반복되는 경우가 가능하다.
int eIdx = -1; 
for (int i = 2; i < sentence.length(); i++) {
	if(checkLetter(sentence, i)) {
		eIdx = i;
		if(sentence.charAt(eIdx) != ch) break;
	}
}
if(eIdx == -1) return "invalid";
				
while(eIdx >= 1) {
	if(ch == sentence.charAt(eIdx)) break;
	eIdx--;
}

4. 단어를 찾으면 단어를 리스트에 넣고, 문자열에서 단어를 잘라내는 작업을 진행한다.

words.add(sentence.substring(0, eIdx+2));
sentence = sentence.substring(eIdx+2);

👍코드

import java.util.ArrayList;
import java.util.List;

public class 브라이언의고민 {

	public static String solution(String sentence) {
		
		List<Integer> pos;
		List<String> words = new ArrayList<>();
		
	
		char ch = ' ';
		while(sentence.length() > 0) {
			if(sentence.length() < 3) return "invalid";
			
			// 규칙2의 경우
			if(checkLetter(sentence, 0)) {
				ch = sentence.charAt(0);
				
				int eIdx = 0; 
				for(int i=1; i<sentence.length(); i++) {
					if(sentence.charAt(i) == ch) {
						eIdx = i;
						break;
					}
				}
				if(eIdx == 0) return "invalid";
				words.add(sentence.substring(0, eIdx+1));
				sentence = sentence.substring(eIdx+1);
			}else {
			
				// 규칙 1의 경우
				if(!checkLetter(sentence, 1)) return "invalid";
				ch = sentence.charAt(1);
				
				int eIdx = -1; 
				for (int i = 2; i < sentence.length(); i++) {
					if(checkLetter(sentence, i)) {
						eIdx = i;
						if(sentence.charAt(eIdx) != ch) break;
					}
				}
				if(eIdx == -1) return "invalid";
				
				
				while(eIdx >= 1) {
					if(ch == sentence.charAt(eIdx)) break;
					eIdx--;
				}
				 
				words.add(sentence.substring(0, eIdx+2));
				sentence = sentence.substring(eIdx+2);
			}
		}

		String ans = "";
		for(String s: words) {
			ans += converToString(s)+" ";
		}
		
		return ans.trim();
	}

	private static String converToString(String s) {
		
		if(checkLetter(s, 0)) s = s.replace(s.charAt(0), ' ');
		if(checkLetter(s, 1)) s = s.replace(s.charAt(1), ' ');
		if(checkLetter(s, 2)) s = s.replace(s.charAt(2), ' ');
		s = s.replace(" ", "");
		
		return s;
	}

	private static boolean checkLetter(String str, int i) {
		if(str.charAt(i) >= 'a' && str.charAt(i) <= 'z') return true;
		return false;
	}

	public static void main(String[] args) {
		System.out.println(solution("bTxTxTaTxTbkABaCDk"));
	}

}


반례를 찾았다


kABaCDk가 잘못나오고 있다. 내일의 나가 해결해 주길... ^^

0개의 댓글