forked from begeekmyfriend/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Leo Ma <[email protected]>
- Loading branch information
1 parent
904e5e8
commit ed37657
Showing
2 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
all: | ||
gcc -O2 -o test scramble_string.c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <stdbool.h> | ||
#include <string.h> | ||
|
||
/* The recursive way is quite simple. | ||
* 1) break the string to two parts: | ||
* s1[0..j] s1[j+1..n] | ||
* s2[0..j] s2[j+1..n] | ||
* 2) then | ||
* isScramble(s1[0..j], s2[0..j]) && isScramble(s1[j+1..n], s2[j+1..n]) | ||
* or | ||
* isScramble(s1[0..j], s2[n - j - 1, n]) && isScramble(s1[j+1..n], s2[0..n - j]) | ||
*/ | ||
#define N 128 | ||
|
||
static bool scramble(char *s1, int low1, int high1, char *s2, int low2, int high2) | ||
{ | ||
if (high1 - low1 != high2 - low2) { | ||
return false; | ||
} else if (!memcmp(s1 + low1, s2 + low2, high1 - low1 + 1)) { | ||
return true; | ||
} else { | ||
int i, c1[N], c2[N]; | ||
memset(c1, 0, N * sizeof(int)); | ||
memset(c2, 0, N * sizeof(int)); | ||
for (i = low1; i <= high1; i++) { | ||
c1[s1[i]]++; | ||
} | ||
for (i = low2; i <= high2; i++) { | ||
c2[s2[i]]++; | ||
} | ||
if (memcmp(c1, c2, N * sizeof(int))) { | ||
return false; | ||
} else { | ||
int len = high1 - low1 + 1; | ||
for (i = 1; i < len; i++) { | ||
if (scramble(s1, low1, low1 + i - 1, s2, low2, low2 + i - 1) && | ||
scramble(s1, low1 + i, high1, s2, low2 + i, high2)) { | ||
return true; | ||
} | ||
if (scramble(s1, low1, low1 + i - 1, s2, high2 - i + 1, high2) && | ||
scramble(s1, low1 + i, high1, s2, low2, high2 - i)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
static bool isScramble(char* s1, char* s2) { | ||
int len1 = strlen(s1); | ||
int len2 = strlen(s2); | ||
return scramble(s1, 0, len1 - 1, s2, 0, len2 - 1); | ||
} | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
if (argc != 3) { | ||
fprintf(stderr, "Usage: ./test s1 s2\n"); | ||
exit(-1); | ||
} | ||
printf("%s\n", isScramble(argv[1], argv[2]) ? "true" : "false"); | ||
return 0; | ||
} |