Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6338108

Browse files
Add C++ implementation
Signed-off-by: begeekmyfriend <begeekmyfriend@gmail.com>
1 parent 3796b8a commit 6338108

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

‎0567_permutation_in_string/Makefile‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
all:
2+
gcc -O1 -o test permutation_in_string.c
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include <stdbool.h>
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
#include <string.h>
5+
6+
7+
bool checkInclusion(char * s1, char * s2)
8+
{
9+
int i, count[128] = { -1 }, pat_len = 0;
10+
for (i = 0; s1[i] != '0円'; i++) {
11+
count[s1[i]]++;
12+
pat_len++;
13+
}
14+
15+
int l = 0, r = 0, len = 0;
16+
while (s2[r] != '0円') {
17+
if (--count[s2[r++]] >= 0) {
18+
len++;
19+
}
20+
while (r - l >= pat_len) {
21+
if (len == pat_len) {
22+
return true;
23+
}
24+
if (++count[s2[l++]] > 0) {
25+
len--;
26+
}
27+
}
28+
}
29+
30+
return false;
31+
}
32+
33+
int main(int argc, char **argv)
34+
{
35+
if (argc != 3) {
36+
fprintf(stderr, "Usage: ./test string pattern\n");
37+
exit(-1);
38+
}
39+
40+
char *t = argv[1];
41+
char *s = argv[2];
42+
printf("%s\n", checkInclusion(t, s) ? "true" : "false");
43+
return 0;
44+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /