• 코드

정답 코드 : wolf의 올바른 순서는 정규식으로 맞는지 틀린지 판단을 했다. 근데 문제는 wolf을 제대로 썼는데 woolf이런식으로 쓴 경우가 문제가 되었다. 그래서 카운트를 세어서 w,o,l,f의 개수가 모두 같을 때 1을 출력하게 했는데 또 문제가 발생을 했다. wwolfwoolfwollfwolff 이런식으로 테스트 데이터를 넣으면 0을 출력해야 되는데, 1을 출력하는 것이었다. 그래서 이 부분은 반복문 안에서 f다음에 w가 올때 그 전 wolf의 개수가 모두 동일한지 판단을 해서 boolean형식으로 체크를 했다.

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
	
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		String S = br.readLine();
		String test = "(w+o+l+f+)+";
		int wCount=0,oCount=0,lCount=0,fCount=0;
		boolean check = false;
		
		for(int i=0;i<S.length();i++) {			
			if(S.charAt(i)=='w')
				wCount++;
			else if(S.charAt(i)=='o')
				oCount++;
			else if(S.charAt(i)=='l')
				lCount++;
			else
				fCount++;
			
			if(i<S.length()-1 && S.charAt(i)=='f' && S.charAt(i+1)=='w') {
				if(!(wCount==oCount && oCount==lCount&& lCount==fCount && fCount == wCount)) {
					check = true;
					break;
				}
			}
		}
		
		if(!check && S.matches(test) && wCount==oCount && oCount==lCount&& lCount==fCount && fCount == wCount)
			System.out.println(1);
		else
			System.out.println(0);
	}
}

+ Recent posts