add: 220207

This commit is contained in:
Lam Haoyin 2022-02-07 18:16:00 +08:00
parent 1c29ae9614
commit ae07a21c67
No known key found for this signature in database
GPG Key ID: 8C089CB1A2B7544F
2 changed files with 32 additions and 1 deletions

31
2202/220207.cpp Normal file
View File

@ -0,0 +1,31 @@
#include <string>
#include <cassert>
#include <iostream>
/**
* 389. 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.
*/
class Solution {
public:
static char findTheDifference(const std::string& s, const std::string& t) {
int c[26]{};
for (char ch : s) {
++c[ch - 'a'];
}
for (char ch : t) {
if (--c[ch - 'a'] < 0)
return ch;
}
assert(false);
return 0;
}
};
int main() {
std::cout << Solution::findTheDifference("abcd", "abcde");
return 0;
}

View File

@ -3,4 +3,4 @@ PROJECT(2202)
SET(CMAKE_CXX_STANDARD 23)
ADD_EXECUTABLE(2202 220207-CN.cpp)
ADD_EXECUTABLE(2202 220207.cpp)