01-09 18:44
Recent Posts
Recent Comments
๊ด€๋ฆฌ ๋ฉ”๋‰ด

miinsun

[Algorithm]์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ž๋ฐ”_31 ์•„๋‚˜๊ทธ๋žจ(ํ•ด์‰ฌ) ๋ณธ๋ฌธ

Algorithm/Java

[Algorithm]์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ž๋ฐ”_31 ์•„๋‚˜๊ทธ๋žจ(ํ•ด์‰ฌ)

miinsun 2022. 1. 5. 19:33

 

๐Ÿ’ฌ ๋ฌธ์ œ ์„ค๋ช…

Anagram์ด๋ž€ ๋‘ ๋ฌธ์ž์—ด์ด ์•ŒํŒŒ๋ฒณ์˜ ๋‚˜์—ด ์ˆœ์„œ๋ฅผ ๋‹ค๋ฅด์ง€๋งŒ ๊ทธ ๊ตฌ์„ฑ์ด ์ผ์น˜ํ•˜๋ฉด ๋‘ ๋‹จ์–ด๋Š” ์•„๋‚˜๊ทธ๋žจ์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค๋ฉด AbaAeCe ์™€ baeeACA ๋Š” ์•ŒํŒŒ๋ฒณ์„ ๋‚˜์—ด ์ˆœ์„œ๋Š” ๋‹ค๋ฅด์ง€๋งŒ
๊ทธ ๊ตฌ์„ฑ์„ ์‚ดํŽด๋ณด๋ฉด A(2), a(1), b(1), C(1), e(2)๋กœ ์•ŒํŒŒ๋ฒณ๊ณผ ๊ทธ ๊ฐœ์ˆ˜๊ฐ€ ๋ชจ๋‘ ์ผ์น˜ํ•ฉ๋‹ˆ๋‹ค.
์ฆ‰ ์–ด๋Š ํ•œ ๋‹จ์–ด๋ฅผ ์žฌ ๋ฐฐ์—ดํ•˜๋ฉด ์ƒ๋Œ€ํŽธ ๋‹จ์–ด๊ฐ€ ๋  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์„ ์•„๋‚˜๊ทธ๋žจ์ด๋ผ ํ•ฉ๋‹ˆ๋‹ค.

๊ธธ์ด๊ฐ€ ๊ฐ™์€ ๋‘ ๊ฐœ์˜ ๋‹จ์–ด๊ฐ€ ์ฃผ์–ด์ง€๋ฉด ๋‘ ๋‹จ์–ด๊ฐ€ ์•„๋‚˜๊ทธ๋žจ์ธ์ง€ ํŒ๋ณ„ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”.
์•„๋‚˜๊ทธ๋žจ ํŒ๋ณ„์‹œ ๋Œ€์†Œ๋ฌธ์ž๊ฐ€ ๊ตฌ๋ถ„๋ฉ๋‹ˆ๋‹ค.

 

 

๐Ÿ”จ ์ž…์ถœ๋ ฅ ์˜ˆ

์ž…๋ ฅ - ์ฒซ ์ค„์— ์ฒซ ๋ฒˆ์งธ ๋‹จ์–ด๊ฐ€ ์ž…๋ ฅ๋˜๊ณ , ๋‘ ๋ฒˆ์งธ ์ค„์— ๋‘ ๋ฒˆ์งธ ๋‹จ์–ด๊ฐ€ ์ž…๋ ฅ๋ฉ๋‹ˆ๋‹ค.

๋‹จ์–ด์˜ ๊ธธ์ด๋Š” 100์„ ๋„˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

์˜ˆ์‹œ ์ž…๋ ฅ 1)

AbaAeCe
baeeACA

์˜ˆ์‹œ ์ž…๋ ฅ 2)

abaCC
Caaab

 

์ถœ๋ ฅ - ๋‘ ๋‹จ์–ด๊ฐ€ ์•„๋‚˜๊ทธ๋žจ์ด๋ฉด “YES"๋ฅผ ์ถœ๋ ฅํ•˜๊ณ , ์•„๋‹ˆ๋ฉด ”NO"๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ์‹œ ์ถœ๋ ฅ 1)

YES

์˜ˆ์‹œ ์ถœ๋ ฅ 2)

NO

โ€‹

๐Ÿ’ป Solution.java

import java.util.*;

public class Main {

	public String solution(String str1, String str2) {
		String answer = "NO";
		HashMap<Character, Integer> map1 = new HashMap<> ();
		HashMap<Character, Integer> map2 = new HashMap<> ();
		
		for(char ch : str1.toCharArray()) {
			map1.put(ch, map1.getOrDefault(ch, 0) + 1);
		}
		
		for(char ch : str2.toCharArray()) {
			map2.put(ch, map2.getOrDefault(ch, 0) + 1);
		}
		
		for(char key : map1.keySet()) {
			if(map1.get(key) != map2.get(key)) {
				return answer;
			}
		}
		
		answer = "YES";
		
        return answer;
	}
	
	public static void main(String[] args){
		Main main  = new Main();
		Scanner sc =new Scanner(System.in);
		
		String str1 = sc.next();
		String str2 = sc.next();
		
		System.out.println(main.solution(str1, str2));
		
		sc.close();
		return ;
	}
}

 

 

 

Comments