티스토리 뷰

You are given two strings s and t.

String t is generated by random shuffling string s and then add one more letter at a random position.

Return the letter that was added to t.

 

Example 1:

Input: s = "abcd", t = "abcde"
Output: "e"
Explanation: 'e' is the letter that was added.

Example 2:

Input: s = "", t = "y"
Output: "y"

 

문제 : String s, t 의 다른 문자를 찾아라

풀이

class Solution {
    public char findTheDifference(String s, String t) {
        
        int[] alphaS = new int[26];
        int[] alphaT = new int[26];
        for(char ch : s.toCharArray()){
            alphaS[ch-'a']++;
        }
        for(char ch : t.toCharArray()){
            alphaT[ch-'a']++;
        }
        
        char ch = ' ';
        for(int i=0;i<26;i++){
            if(alphaS[i] != alphaT[i]) return (char)(i+(int)'a');
        }
        
        return ch;
    }
}

 

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/07   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
글 보관함