티스토리 뷰
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;
}
}
'알고리즘' 카테고리의 다른 글
leetcode525. Contiguous Array (0) | 2022.02.07 |
---|---|
leetcode - 80. Remove Duplicates from Sorted Array II (0) | 2022.02.06 |
leetcode - 23. Merge k Sorted Lists (0) | 2022.02.05 |
leetcode - 121. Best Time to Buy and Sell Stock (0) | 2022.02.01 |
leetcode - 134. Gas Station (0) | 2022.01.25 |