diff --git a/2202/220207.cpp b/2202/220207.cpp new file mode 100644 index 0000000..1649b42 --- /dev/null +++ b/2202/220207.cpp @@ -0,0 +1,31 @@ +#include +#include +#include + +/** + * 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; +} diff --git a/2202/CMakeLists.txt b/2202/CMakeLists.txt index 7a81450..717308c 100644 --- a/2202/CMakeLists.txt +++ b/2202/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2202) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2202 220207-CN.cpp) +ADD_EXECUTABLE(2202 220207.cpp)