add: 220207
This commit is contained in:
parent
1c29ae9614
commit
ae07a21c67
|
|
@ -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;
|
||||
}
|
||||
|
|
@ -3,4 +3,4 @@ PROJECT(2202)
|
|||
|
||||
SET(CMAKE_CXX_STANDARD 23)
|
||||
|
||||
ADD_EXECUTABLE(2202 220207-CN.cpp)
|
||||
ADD_EXECUTABLE(2202 220207.cpp)
|
||||
|
|
|
|||
Loading…
Reference in New Issue