본문 바로가기

알고리즘/알고리즘

[백준] No Duplicates

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

 

15098번: No Duplicates

Input is a line containing words separated by single spaces, where a word consists of one or more uppercase letters. A line contains no more than 80 characters.

www.acmicpc.net

 

 

 

package com.company;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.HashMap;

public class test19 {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        String[] arr = br.readLine().split(" ");
        HashMap<String, Integer> hashMap = new HashMap<String, Integer>();
        for(int i=0; i<arr.length; i++){
            String temp = arr[i];
            Integer tempValue = hashMap.get(temp);//null
            hashMap.put(temp, tempValue == null ? 1 : tempValue+1);
        }
        //System.out.println(hashMap.entrySet());// [THE=2, RAIN=1, IN=1, SNOW=1, AND=1]
        //System.out.println(hashMap.values());// [2, 1, 1, 1, 1]
        Integer maxValue = Collections.max(hashMap.values());// 최대값
        if(maxValue>1){
            System.out.println("no");
        }else System.out.println("yes");
    }//main
}

 

포인트

 

hashMap 에 처음 key넣으면 값은 null

tempValue == null ? 1 : tempValue+1)

Integer maxValue = Collections.max(hashMap.values());// 최대값 for문 대신에 사용하면 코드 길이가 짧아짐

 

 

hashMap 이지만 contains로 확인

/* 
 * Author: Minho Kim (ISKU)
 * Date: 2017.12.26
 * E-mail: minho1a@hanmail.net
 * 
 * https://github.com/ISKU/Algorithm
 * https://www.acmicpc.net/problem/15098
 */

import java.util.*;

public class Main {
	public static void main(String... args) {
		Scanner input = new Scanner(System.in);
		HashSet<String> set = new HashSet<String>();

		while (input.hasNext()) {
			String word = input.next();
			if (set.contains(word)) {
				System.out.print("no");
				System.exit(0);
			}
			set.add(word);
		}

		System.out.print("yes");
	}
}

 

 

다른 사람 . token으로 check 기능 사용

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.HashSet;
import java.util.StringTokenizer;

public class Main {
	public static void main(String[] args) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
		StringTokenizer st = new StringTokenizer(br.readLine());
		HashSet<String> hs = new HashSet<String>();
		boolean chk = true;
		while (st.hasMoreTokens()) {
			if (!hs.add(st.nextToken()))
				chk = false;
		}
		bw.write((chk?"yes":"no") + "\n");			
		bw.flush();
		bw.close();
		br.close();
	}	
}

'알고리즘 > 알고리즘' 카테고리의 다른 글

[프로그래머스][정렬][k번째 수]  (0) 2021.09.28
[프로그래머스][해시] 완주하지 못한 선수  (0) 2021.09.27
[백준] 숫자의 개수  (0) 2021.09.27
[백준] 평균은 넘겠지  (0) 2021.09.27
[백준] 최댓값  (0) 2021.09.25