[백준] 1361번: 그룹 단어 체커 - JAVA

2024. 9. 17. 02:50·알고리즘

[작성일: 2023. 09. 11]

 

https://www.acmicpc.net/problem/1316

 

 

풀이

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        int count = 0;

        for (int i = 0; i < num; i++) {
            boolean[] alphabet = new boolean[26]; // 각 문자열마다 알파벳 초기화
            String str = sc.next(); // 문자열 입력 받기
            char preChar = str.charAt(0); // 첫(전) 글자 저장
            alphabet[preChar - 97] = true; // 알바펫 자리를 true로 초기화

            boolean check = true;  // check 변수

            for (int j = 1; j < str.length(); j++) {
                char currentChar = str.charAt(j);  // 현재 글자 저장

                if (preChar != currentChar && alphabet[currentChar - 97]) {
                    // 그룹문자가 아닐 때는 count하지 않기 위해 check를 false로 변경
                    // 전 글자와 현재 글자가 같지 않거나, 알파벳 배열이 true일 때
                    // 예를 들어 a b a 일 경우에 해당됨. 전 글자와는 같지 않지만 이전에 나타난 알파벳임.
                    check = false;
                    break;
                }
                // 그룹 문자일 때는 알파벳 배열을 true로 변경하고 전 문자에 현 문자를 대입함.
                alphabet[currentChar - 97] = true;
                preChar = currentChar;

            }
            // check가 true일 때만 count++;
            if (check) {
                count++;
            }
        }

        System.out.println(count);
    }
}

 

 

저작자표시 비영리 변경금지 (새창열림)
'알고리즘' 카테고리의 다른 글
  • [백준] 2563번: 색종이 - JAVA
  • [백준] 2566번: 최댓값 - JAVA
  • [백준] 10988번: 팰린드롬인지 확인하기 - JAVA
  • [백준] 2444번: 별 찍기(7) - JAVA
뚜비
뚜비
1년차 백엔드&iOS 개발자의 감자 탈출 블로그 🥔🥔
  • 뚜비
    뚜비의 개발로그
    뚜비
  • 전체
    오늘
    어제
  • 글쓰기     관리
    • Devlog
      • Back-End
        • Java
        • Spring
        • JPA
        • HTTP
        • Security
        • Back-End
        • Front-End
      • 알고리즘
      • iOS
        • Swift
      • Database
      • Tips
        • Git & GitHub
        • A to Z
      • 프로젝트
      • 생각정리
  • 태그

    김영한
    알고리즘
    의존성주입
    Java
    MVC
    생성자
    spring
    Database
    프로그래머스
    백준
    JPA
    변수
    자바스크립트
    jsp
    객체
    DB
    Spring Security
    sql
    성능최적화
    Security
    javascript
    HTTP
    스프링
    게시판만들기
    html
    최주호
    다형성
    자바
    데이터베이스
    Swift
  • hELLO· Designed By정상우.v4.10.0
뚜비
[백준] 1361번: 그룹 단어 체커 - JAVA
상단으로

티스토리툴바