1) 학급 회장
설명
package com.company.hashmap;
import java.util.HashMap;
import java.util.Scanner;
public class ClassPresident {
public static void main(String [] args) {
ClassPresident classPresident = new ClassPresident();
Scanner in = new Scanner(System.in);
int count = in.nextInt();
String str = "";
str = in.next();
classPresident.solution(count, str);
}
void solution(int count, String str) {
int max = Integer.MIN_VALUE;
char leader = 'N';
char [] arr = str.toCharArray();
HashMap<Character, Integer> vote = new HashMap<>();
for (char c : arr) {
vote.put(c, vote.getOrDefault(c, 0)+1);
}
for (Character character : vote.keySet()) {
int value = vote.get(character);
if (max<value) {
leader = character;
max = value;
}
}
System.out.println(leader);
}
}
Java
복사
2) 아나그램
설명
public class Anagram {
public static void main(String [] args) {
Anagram anagram = new Anagram();
Scanner in = new Scanner(System.in);
String str1 = in.next();
String str2 = in.next();
anagram.solution(str1, str2);
}
void solution(String str1, String str2) {
HashMap<Character, Integer> map = new HashMap<>();
String result = "YES";
char[] arr1 = str1.toCharArray();
for (char c : arr1) {
map.put(c, map.getOrDefault(c,0)+1);
}
char[] arr2 = str2.toCharArray();
for (char c : arr2) {
map.put(c, map.get(c)-1);
}
for (Character character : map.keySet()) {
if(map.get(character) != 0) {
result = "NO";
}
}
System.out.println(result);
}
}
Java
복사
3) 매출액의 종류
설명
package com.company.hashmap;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
public class SaleType {
public static void main(String [] args) {
SaleType saleType = new SaleType();
Scanner in = new Scanner(System.in);
int count = in.nextInt();
int k = in.nextInt();
int [] arr = new int[count];
for (int i=0; i<count; i++) {
arr[i] = in.nextInt();
}
saleType.solution(count, k, arr);
}
void solution(int count, int k, int [] arr) {
ArrayList<Integer> answer = new ArrayList<>();
for (Integer integer : answer) {
}
HashMap<Integer, Integer> map = new HashMap<>();
for (int i=0; i<k-1; i++) {
map.put(arr[i], map.getOrDefault(arr[i],0)+1);
}
int lt = 0;
for (int rt=k-1; rt<count; rt++) {
map.put(arr[rt], map.getOrDefault(arr[rt],0)+1);
answer.add(map.size());
map.put(arr[lt], map.getOrDefault(arr[lt],0)-1);
int getLtval = map.get(arr[lt]);
if (getLtval == 0) {
map.remove(arr[lt]);
}
lt++;
}
for (Integer integer : answer) {
System.out.println(integer);
}
}
}
Java
복사
4) 모든 아나그램 찾기
설명
package com.company.hashmap;
import java.util.HashMap;
import java.util.Scanner;
public class SearchAllAnagram {
public static void main(String [] args) {
SearchAllAnagram searchAllAnagram = new SearchAllAnagram();
Scanner in = new Scanner(System.in);
String str1 = in.next();
String str2 = in.next();
searchAllAnagram.solution(str1, str2);
}
void solution(String str1 ,String str2) {
int answer = 0;
HashMap<Character, Integer> map = new HashMap<>();
HashMap<Character, Integer> map2 = new HashMap<>();
char [] arr1 = str1.toCharArray();
char [] arr2 = str2.toCharArray();
for (char c : arr2) {
map2.put(c, map.getOrDefault(c, 0)+1);
}
for (int i=0; i<arr2.length-1; i++) {
map.put(arr1[i], map.getOrDefault(arr1[i], 0)+1);
}
int lt = 0;
for (int rt = arr2.length-1; rt<arr1.length; rt++) {
map.put(arr1[rt], map.getOrDefault(arr1[rt], 0)+1);
if (map.equals(map2)) {
answer++;
}
map.put(arr1[lt], map.getOrDefault(arr1[lt], 0)-1);
if (map.get(arr1[lt]) == 0) {
map.remove(arr1[lt]);
}
lt++;
}
System.out.println(answer);
}
}
Java
복사
5) K번째 큰수
설명
package com.company.hashmap;
import java.util.Comparator;
import java.util.Scanner;
import java.util.TreeSet;
public class LargestNumber {
public static void main(String [] args) {
LargestNumber largestNumber = new LargestNumber();
Scanner in = new Scanner(System.in);
int count = in.nextInt();
int k = in.nextInt();
int [] arr = new int[count];
for (int i=0; i<count; i++) {
arr[i] = in.nextInt();
}
largestNumber.solution(count, k, arr);
}
void solution(int count, int k, int [] arr) {
int answer = -1;
int numberCount = 0;
TreeSet<Integer> tSet = new TreeSet<>(Comparator.reverseOrder());
for (int i=0; i<count; i++) {
for (int j=i+1; j<count; j++) {
for (int l=j+1; l<count; l++) {
tSet.add(arr[i] + arr[j] + arr[l]);
}
}
}
for (Integer integer : tSet) {
numberCount++;
if (numberCount == k) {
answer = integer;
}
}
System.out.println(answer);
}
}
Java
복사