26개의 알파벳 사용 여부 체크 배열 선언(boolean type)
앞의 문자를 저장할 prev 변수 선언(int type)
문자열 길이만큼 반복문 돌면서 앞선 문자(prev)와 현재 문자(now)가 연속되는지 확인
package $01_문자열;
import java.util.Scanner;
public class $01_그룹_단어_체커_1316_실버5 {
public static boolean solution(String s) {
boolean status = true;
boolean[] alphabet = new boolean[26];
int prev = -1;
for(int i = 0 ; i < s.length() ; i++) {
int now = s.charAt(i) - 'a';
if(prev != now) {
if(alphabet[now]) {
status = false;
return status;
}else {
alphabet[now] = true;
}
}
prev = now;
}
return status;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int count = 0;
for(int i = 0 ; i < N ; i++) {
String s = sc.next();
if(solution(s)) {
System.out.println(s);
count++;
}
}
System.out.println(count);
sc.close();
}
}