add: 230409-CN

This commit is contained in:
Eatswap 2023-04-09 13:43:07 +08:00
parent 31043a54dc
commit 1ff4e631c2
Signed by: Eatswap
GPG Key ID: BE661106A1F3FA0B
2 changed files with 37 additions and 1 deletions

36
cpp/2304/230409-CN.cpp Normal file
View File

@ -0,0 +1,36 @@
#include <string>
#include <vector>
#include <iostream>
/**
* 2399. Check Distances Between Same Letters
*
* You are given a 0-indexed string s consisting of only lowercase English letters, where each letter in s appears exactly twice. You are also given a 0-indexed integer array distance of length 26.
* Each letter in the alphabet is numbered from 0 to 25 (i.e. 'a' -> 0, 'b' -> 1, 'c' -> 2, ... , 'z' -> 25).
* In a well-spaced string, the number of letters between the two occurrences of the ith letter is distance[i]. If the ith letter does not appear in s, then distance[i] can be ignored.
* Return true if s is a well-spaced string, otherwise return false.
*/
class Solution {
public:
static bool checkDistances(const std::string&, const std::vector<int>&);
};
bool Solution::checkDistances(const std::string& s, const std::vector<int>& distance) {
int pos[26]{};
std::memset(pos, -1, sizeof pos);
for (int i = 0; i < s.length(); ++i) {
int& p = pos[s[i] - 'a'];
p = i + (p < 0 ? 1 : -p);
}
for (int i = 0; i < 26; ++i)
if (pos[i] >= 0 && pos[i] != distance[i])
return false;
return true;
}
int main() {
std::cout << Solution::checkDistances("abaccb", {
1,3,0,5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
});
}

View File

@ -4,4 +4,4 @@ PROJECT(2304)
SET(CMAKE_CXX_STANDARD 23) SET(CMAKE_CXX_STANDARD 23)
SET(CMAKE_EXPORT_COMPILE_COMMANDS true) SET(CMAKE_EXPORT_COMPILE_COMMANDS true)
ADD_EXECUTABLE(2304 230408.cpp) ADD_EXECUTABLE(2304 230409-CN.cpp)