2022-02-07 Daily-Challenge
Today I have done leetcode's February LeetCoding Challenge with cpp
.
February LeetCoding Challenge 7
Description
Find the Difference
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"
Constraints:
0 <= s.length <= 1000
t.length == s.length + 1
s
andt
consist of lowercase English letters.
Solution
auto speedup = [](){
cin.tie(nullptr);
cout.tie(nullptr);
ios::sync_with_stdio(false);
return 0;
}();
class Solution {
public:
char findTheDifference(string s, string t) {
int count[26] = {};
for(auto c : s) {
count[c - 'a'] += 1;
}
for(auto c : t) {
count[c - 'a'] -= 1;
}
for(int i = 0; i < 26; ++i) {
if(count[i]) return i + 'a';
}
return 0;
}
};
// Accepted
// 54/54 cases passed (0 ms)
// Your runtime beats 100 % of cpp submissions
// Your memory usage beats 87.58 % of cpp submissions (6.6 MB)