From ae07a21c67e0b78c8ab17a9b7026f70c34d4ee18 Mon Sep 17 00:00:00 2001 From: Lam Haoyin Date: Mon, 7 Feb 2022 18:16:00 +0800 Subject: [PATCH] add: 220207 --- 2202/220207.cpp | 31 +++++++++++++++++++++++++++++++ 2202/CMakeLists.txt | 2 +- 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 2202/220207.cpp 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)