본문 바로가기

JAVA

BAEKJOON(백준) 문자열 - 1316번 JAVA CODE

정답

package BaekJoon;

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

public class Main {
	
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		char[] sp;
	
		int N = Integer.parseInt(br.readLine());
		
		String str;
		int last_idx = -1;
		int rear_idx = -1;
		int count = N;

		for(int i=0; i<N; i++) {
			str = br.readLine();
			sp = str.toCharArray();
			for(int l=0; l<sp.length; l++) {
				last_idx = -1;
				rear_idx = -1;
				for(int k=l+1; k<sp.length; k++) {
					if(sp[l] == sp[k]) {
						rear_idx = l;
						last_idx = k;
						break;
					}
				}
				//System.out.println("rear : " + rear_idx);
				//System.out.println("last : " + last_idx);
				if(last_idx - rear_idx != 1 && last_idx != rear_idx) {
					count--;
					break;
				}
			}
		}
		
		System.out.println(count);
	}

 

 

풀다 만 거

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

public class Main {
	
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		char[] sp;
	
		int N = Integer.parseInt(br.readLine());
		
		String str;
		int count = N;
		Boolean h = false;

		char[] arr = new char[100];
		int num = 0;
		
		for(int i=0; i<N; i++) {
			str = br.readLine();
			sp = str.toCharArray();
			
			for(int k=1; k<sp.length; k++) {
				if(k == 1) {
					if(sp[k] != sp[k-1])
						arr[num] = sp[k-1];
					num++;
				}
				if(sp[k] != sp[k-1]) {
					arr[num] = sp[k];
					num++;
				}
			}
			
			for(int j=0; j<num; j++) {
				for(int l=j+1; l<num; l++) {
					if(arr[j] == arr[l] && j != l) {
						count--;
						h = true;
						break;
					}
					if (h == true)
						break;
				}
			}
			
			/*
			for(int n=0; n<num; n++) {
				System.out.println("arr : " + arr[n]);
			}
			*/
			//System.out.println(count);
			h = false;
			num = 0;
		}
		
		System.out.println(count);
	}
}