【
All DNA by a series of abbreviated as A, C, G, and T nucleotides, for example: "ACGAATTCCG". In the study of DNA, DNA repeat sequence recognition in the research will be very helpful sometimes.
A write function to find more than once a sequence of 10 characters long (substring) the DNA molecule in all occurrences.
Example:
Input: s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
Output: [ "AAAAACCCCC", "CCCCCAAAAA "]
Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/repeated-dna-sequences
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.
】
This question by treatment with 31/32 cases, and finally with a timeout embodiment. Bit by bit hash estimate should do some first recorded version of this use case does not pass than it will save a hash string.
This question is just to see would feel unable to start. Then look to see the number of such fields, should think of is to use a hash.
So how correspondence between the hash key structure it? Thinking is traversed in the string s, if not recorded, if there will count by one. I.e., the keys are equal, a further need to increase the count array.
This problem to use string comparison function strcmp, string copy function strncpy and strcpy.
Then a large part of the application code is in the malloc memory, C language brush title, this point still have to endure.
int count;
void Checkhash(char *temp, char **hashstr, char *countarr) {
int i;
for (i = 0; i < count; i++) {
if (!strcmp(temp, hashstr[i])) {
countarr[i] = 1; // 找到重复
return;
}
}
strcpy(hashstr[count], temp);
count++;
}
char ** findRepeatedDnaSequences(char * s, int* returnSize){
int i;
char **retarr;
int len = strlen(s);
char **hashstr;
char temp[11] = {0};
char countarr[len + 1];
int retcount = 0;
count = 0;
memset(countarr, 0, (len + 1));
hashstr = (char **)malloc(sizeof(char *) * len);
for (i = 0; i < len; i++) {
hashstr[i] = (char *)malloc(sizeof(char) * 11);
memset(hashstr[i], 0 , sizeof(char) * 11);
}
retarr = (char **)malloc(sizeof(char *) * len);
for (i = 0; i < len; i++) {
retarr[i] = (char *)malloc(sizeof(char) * 11);
}
for (i = 0; i < len - 9; i++) {
strncpy(temp, s + i, 10);
Checkhash(temp, hashstr, countarr);
}
for (i = 0 ; i < count; i++) {
if (countarr[i] > 0) {
strcpy(retarr[retcount], hashstr[i]);
retcount++;
}
}
*returnSize = retcount;
return retarr;
}