Skip to content

Commit

Permalink
feat: update lc-1366
Browse files Browse the repository at this point in the history
Signed-off-by: Certseeds <[email protected]>
  • Loading branch information
Certseeds committed Aug 30, 2023
1 parent 4183cb5 commit 6fdbba3
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 5 deletions.
2 changes: 1 addition & 1 deletion algorithm/string/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ set(dependencies 3 6 8 12 13)
LIST(APPEND dependencies 14 22 38 344 535)
LIST(APPEND dependencies 657 387 383 so_05 242)
LIST(APPEND dependencies so_58 709 763 771 784)
LIST(APPEND dependencies 1324 1328 1358)
LIST(APPEND dependencies 1324 1328 1358 1366)
LIST(TRANSFORM dependencies PREPEND leetcode_)

foreach (elementName IN LISTS dependencies)
Expand Down
8 changes: 4 additions & 4 deletions algorithm/string/leetcode_1358.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/*
CS203_DSAA_template
Copyright (C) 2022 nanoseeds
Copyright (C) 2022-2023 nanoseeds
*/
#include "leetcode_1358_test.hpp"
Expand All @@ -11,15 +11,15 @@ namespace leetcode_1358 {

int32_t leetcode_1358::numberOfSubstrings(const string &s) {
int32_t count{0};
int32_t a{0}, b{0}, c{0};
for (size_t left{0}, right{0}; right < s.size();) {
const auto s_size{s.size()};
for (size_t left{0}, right{0}, a{0}, b{0}, c{0}; right < s_size;) {
const auto ch1 = s[right];
if (ch1 == 'a') { a += 1; }
if (ch1 == 'b') { b += 1; }
if (ch1 == 'c') { c += 1; }
right += 1;
while (a >= 1 && b >= 1 && c >= 1) {
count += s.size() - right + 1;
count += s_size - right + 1;
const auto ch2 = s[left];
if (ch2 == 'a') { a -= 1; }
if (ch2 == 'b') { b -= 1; }
Expand Down
54 changes: 54 additions & 0 deletions algorithm/string/leetcode_1366.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
/*
CS203_DSAA_template
Copyright (C) 2023 nanoseeds
*/
#include "leetcode_1366_test.hpp"

namespace leetcode_1366 {

string leetcode_1366::rankTeams(const vector<string> &votes) {
if (votes.empty() || votes.front().empty()) {
return {};
}
struct sorted {
std::vector<int32_t> vec_votes{};
char ch{'A'};
int16_t count{0};
};
const auto votes_size{votes.size()};
const auto vote_of_each{votes.front().size()};
constexpr const auto groups{26};
vector<sorted> voted_array(groups);
for (size_t i{0}; i < groups; ++i) {
voted_array[i].ch += i;
voted_array[i].vec_votes.resize(vote_of_each);
}
for (size_t i{0}; i < vote_of_each; ++i) {
for (size_t j{0}; j < votes_size; ++j) {
const auto ch{votes[j][i] - 'A'};
voted_array[ch].vec_votes[i] += 1;
voted_array[ch].count += 1;
}
}
// or can filter count = 0 in there
std::sort(voted_array.begin(), voted_array.end(), [vote_of_each](auto obj1, auto obj2) {
for (size_t i{0}; i < vote_of_each; ++i) {
if (obj1.vec_votes[i] != obj2.vec_votes[i]) {
return obj1.vec_votes[i] > obj2.vec_votes[i];
}
}
return obj1.ch < obj2.ch;
});
string will_return{};
for (const auto &voted: voted_array) {
if (voted.count != 0) {
will_return += voted.ch;
}
}
return will_return;
}

}
92 changes: 92 additions & 0 deletions algorithm/string/leetcode_1366_test.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
/*
CS203_DSAA_template
Copyright (C) 2023 nanoseeds
*/
//@Tag string
//@Tag 字符串

#ifndef CS203_DSAA_TEMPLATE_ALGORITHM_STRING_LEETCODE_1366_TEST_HPP
#define CS203_DSAA_TEMPLATE_ALGORITHM_STRING_LEETCODE_1366_TEST_HPP

#include <catch_main.hpp>
#include <cstdint>
#include <cstddef>
#include <string>

namespace leetcode_1366 {
using std::string;

namespace leetcode_1366 {
string rankTeams(const vector<string> &votes);

}

TEST_CASE("1 [test_1366]", "[test_1366]") {
const vector<string> input{"ABC", "ACB", "ABC", "ACB", "ACB"};
const char *const result{"ACB"};
CHECK(result == leetcode_1366::rankTeams(input));
}

TEST_CASE("2 [test_1366]", "[test_1366]") {
const vector<string> input{"WXYZ", "XYZW"};
const char *const result{"XWYZ"};
CHECK(result == leetcode_1366::rankTeams(input));
}


TEST_CASE("3 [test_1366]", "[test_1366]") {
const vector<string> input{"ZMNAGUEDSJYLBOPHRQICWFXTVK"};
const char *const result{"ZMNAGUEDSJYLBOPHRQICWFXTVK"};
CHECK(result == leetcode_1366::rankTeams(input));
}


TEST_CASE("4 [test_1366]", "[test_1366]") {
const vector<string> input{"BCA", "CAB", "CBA", "ABC", "ACB", "BAC"};
const char *const result{"ABC"};
CHECK(result == leetcode_1366::rankTeams(input));
}


TEST_CASE("5 [test_1366]", "[test_1366]") {
const vector<string> input{"M", "M", "M", "M"};
const char *const result{"M"};
CHECK(result == leetcode_1366::rankTeams(input));
}

TEST_CASE("6 [test_1366]", "[test_1366]") {
const vector<string> input{ "FVSHJIEMNGYPTQOURLWCZKAX",
"AITFQORCEHPVJMXGKSLNZWUY",
"OTERVXFZUMHNIYSCQAWGPKJL",
"VMSERIJYLZNWCPQTOKFUHAXG",
"VNHOZWKQCEFYPSGLAMXJIUTR",
"ANPHQIJMXCWOSKTYGULFVERZ",
"RFYUXJEWCKQOMGATHZVILNSP",
"SCPYUMQJTVEXKRNLIOWGHAFZ",
"VIKTSJCEYQGLOMPZWAHFXURN",
"SVJICLXKHQZTFWNPYRGMEUAO",
"JRCTHYKIGSXPOZLUQAVNEWFM",
"NGMSWJITREHFZVQCUKXYAPOL",
"WUXJOQKGNSYLHEZAFIPMRCVT",
"PKYQIOLXFCRGHZNAMJVUTWES",
"FERSGNMJVZXWAYLIKCPUQHTO",
"HPLRIUQMTSGYJVAXWNOCZEKF",
"JUVWPTEGCOFYSKXNRMHQALIZ",
"MWPIAZCNSLEYRTHFKQXUOVGJ",
"EZXLUNFVCMORSIWKTYHJAQPG",
"HRQNLTKJFIEGMCSXAZPYOVUW",
"LOHXVYGWRIJMCPSQENUAKTZF",
"XKUTWPRGHOAQFLVYMJSNEIZC",
"WTCRQMVKPHOSLGAXZUEFYNJI"};
const char *const result{"VWFHSJARNPEMOXLTUKICZGYQ"};
CHECK(result == leetcode_1366::rankTeams(input));
}




}
#endif //CS203_DSAA_TEMPLATE_ALGORITHM_STRING_LEETCODE_1366_TEST_HPP

0 comments on commit 6fdbba3

Please sign in to comment.