-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpermutation_in_string.cpp
More file actions
55 lines (50 loc) · 1.33 KB
/
permutation_in_string.cpp
File metadata and controls
55 lines (50 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
* =====================================================================================
*
* Filename: permutation_in_string.cpp
*
* Description: 567. Permutation in String
* https://leetcode.com/problems/permutation-in-string/
*
* Version: 1.0
* Created: 02/11/2025 13:25:35
* Revision: none
* Compiler: gcc
*
* Author: xianfeng.zhu@gmail.com
* Organization:
*
* =====================================================================================
*/
#include <string>
#include <tuple>
#include <vector>
#include "gtest/gtest.h"
using std::string;
// Sorting
class Solution {
public:
bool checkInclusion(string s1, string s2) {
if (s1.length() > s2.length()) {
return false;
}
std::sort(s1.begin(), s1.end());
for (int i = 0; i <= s2.length() - s1.length(); i++) {
string ss = s2.substr(i, s1.length());
std::sort(ss.begin(), ss.end());
if (ss == s1) {
return true;
}
}
return false;
}
};
TEST(Solution, checkInclusion) {
std::vector<std::tuple<string, string, bool>> cases = {
std::make_tuple("ab", "eidbaooo", true),
std::make_tuple("ab", "eidboaoo", false),
};
for (auto& c : cases) {
EXPECT_EQ(Solution().checkInclusion(std::get<0>(c), std::get<1>(c)), std::get<2>(c));
}
}