結果

問題 No.69 文字を自由に並び替え
ユーザー ShotaroSuzuShotaroSuzu
提出日時 2020-05-19 20:15:10
言語 Java21
(openjdk 21)
結果
AC  
実行時間 108 ms / 5,000 ms
コード長 1,374 bytes
コンパイル時間 2,972 ms
コンパイル使用メモリ 78,652 KB
実行使用メモリ 54,176 KB
最終ジャッジ日時 2024-04-09 22:24:00
合計ジャッジ時間 5,193 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 105 ms
54,016 KB
testcase_01 AC 108 ms
54,132 KB
testcase_02 AC 106 ms
54,120 KB
testcase_03 AC 108 ms
54,112 KB
testcase_04 AC 103 ms
54,064 KB
testcase_05 AC 92 ms
54,176 KB
testcase_06 AC 103 ms
53,916 KB
testcase_07 AC 95 ms
52,912 KB
testcase_08 AC 104 ms
53,612 KB
testcase_09 AC 102 ms
53,008 KB
testcase_10 AC 94 ms
52,832 KB
testcase_11 AC 95 ms
53,060 KB
testcase_12 AC 102 ms
54,092 KB
testcase_13 AC 103 ms
53,948 KB
testcase_14 AC 99 ms
53,112 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package yukicoder.beginner.anagram;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;

public class AnagramJudger {

	public static void main(String[] args) {
		new AnagramJudger().executeJudge();
	}

	private void executeJudge() {
		@SuppressWarnings("resource")
		Scanner sc = new Scanner(System.in);
		String firstString = sc.next();
		String secondString = sc.next();

		boolean res = judge(firstString, secondString);
		if(res) {
			System.out.println("YES");
		} else {
			System.out.println("NO");
		}

	}

	private boolean judge(String firstString, String secondString) {
		Map<Character, Integer> firstStrNumMap = paseToStrNumMap(firstString);
		Map<Character, Integer> secondStrNumMap = paseToStrNumMap(secondString);

		for (Entry<Character, Integer> strNumPair : firstStrNumMap.entrySet()) {
			if(secondStrNumMap.containsKey(strNumPair.getKey()) == false) {
				return false;
			}
			if(secondStrNumMap.getOrDefault(strNumPair.getKey(), -1) != strNumPair.getValue()) {
				return false;
			}
		}
		return true;
	}

	private Map<Character, Integer> paseToStrNumMap(String firstString) {
		Map<Character, Integer> res = new HashMap<>();
		for (int i = 0; i < firstString.length(); i++) {
			Character target = firstString.charAt(i);
			res.put(target, res.getOrDefault(target, 0) + 1);
		}
		return res;
	}

}
0