[코테 풀이] Valid Anagram

시내·2024년 6월 7일
0

Q_242) Valid Anagram

출처 : https://leetcode.com/problems/valid-anagram/?envType=study-plan-v2&envId=programming-skills

Given two strings s and t, return true if t is an anagram of s, and false otherwise.

An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.

class Solution {
    public boolean isAnagram(String s, String t) {
        ArrayList<Character> ArrayListofS = new ArrayList<>();
        ArrayList<Character> ArrayListofT = new ArrayList<>();

        for (int ss = 0; ss < s.length(); ss++) {
            ArrayListofS.add(s.charAt(ss));
        }

        for (int tt = 0; tt < t.length(); tt++) {
            ArrayListofT.add(t.charAt(tt));
        }

        for (Character c : ArrayListofS) {
            if (!ArrayListofT.contains(c)) {
                return false;
            } else {
                ArrayListofT.remove(c);
            }
        }
        if (ArrayListofT.size() >= 1) {
            return false;
        }
        return true;
    }
}
profile
contact 📨 ksw08215@gmail.com

0개의 댓글