0% found this document useful (0 votes)
13 views6 pages

Backtracking

Uploaded by

soni.arun001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views6 pages

Backtracking

Uploaded by

soni.arun001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

1.

Letter Combination of a phone number


char *letters[8] = {"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
void backtrack(char **result, char *digits, char *tmp, int index,
int *resLen);
char ** letterCombinations(char * digits, int* returnSize){

char **result = (char **)malloc(sizeof(char *) * 4000);


char tmp[strlen(digits) + 1];
int resLen = 0;
if (strlen(digits) == 0) {
*returnSize = resLen;
return result;
}
backtrack(result, digits, tmp, 0, &resLen);
*returnSize = resLen;
return result;
}

void backtrack(char **result, char *digits, char *tmp, int index, int *resLen)
{

if (index == strlen(digits)) {
tmp[index] = '\0';
result[(*resLen)] = (char *)malloc(strlen(tmp) + 1);
strcpy(result[(*resLen)], tmp);
(*resLen)++;
return;

}
char *possibleLetters = letters[digits[index] - '0' - 2];
for (int i = 0; i < strlen(possibleLetters); i++) {
tmp[index] = possibleLetters[i];
backtrack(result, digits, tmp, index + 1, resLen);
}
}
2. Generate Parenthesis

void backtrack(char **result, char *tmp, int open, int close, int max, int idx,
int *resLen);
char ** generateParenthesis(int n, int* returnSize){

char **result = (char **)malloc(sizeof(char *) * 1500);


char tmp[n*2 + 1];
int resLen = 0;
if (n == 0) {
*returnSize = resLen;
return result;
}
backtrack(result, tmp, 0, 0, n, 0, &resLen);
*returnSize = resLen;
return result;
}

void backtrack(char **result, char *tmp, int open, int close, int max, int idx,
int *resLen){

if (idx == max * 2) {
tmp[idx] = '\0';
result[(*resLen)] = (char *)malloc(strlen(tmp) + 1);
strcpy(result[(*resLen)], tmp);
(*resLen)++;
return;

}
if (open < max) {
tmp[idx] = '(';
//idx++;
backtrack(result, tmp, open+1, close, max, idx+1, resLen);
}
if (close < open) {
tmp[idx] = ')';
//int idx++;
backtrack(result, tmp, open, close+1, max, idx+1, resLen);
}
}
3. Permutations
void backtrack(int* nums, int numsSize, int **output, int *count, int first,
int** returnColumnSizes){
if(first == numsSize){
output[*count] = (int *)malloc(sizeof(int) * numsSize);
memcpy(output[*count], nums, numsSize * sizeof(int));
*returnColumnSizes = realloc(*returnColumnSizes, sizeof(int) * (*count
+ 1));
(*returnColumnSizes)[*count]=numsSize;
(*count)++;
}
for(int i = first; i < numsSize; i++){
swap(&nums[first], &nums[i]);
backtrack(nums, numsSize, output, count, first + 1, returnColumnSizes);
swap(&nums[first], &nums[i]);
}
}
int** permute(int* nums, int numsSize, int* returnSize, int**
returnColumnSizes){
int **output = NULL;
int **rerurnColumnSizes = NULL;

int count = 0;
*returnSize = count;
if(!nums)
return output;

output = (int **)malloc(sizeof(int *)*1000);


*returnColumnSizes=(int *)malloc(sizeof(int));

backtrack(nums, numsSize, output, &count, 0, returnColumnSizes);


*returnSize=count;
return output;
}
4. Subsets

void backtrack(int* nums, int numsSize, int** res, int* returnSize, int**
returnColumnSizes, int* tmp, int tarSize, int cidx, int start) {
if (cidx == tarSize) {
(*returnColumnSizes)[(*returnSize)] = cidx;
res[(*returnSize)] = (int*) malloc(cidx * sizeof(int));
if (tarSize) memcpy(res[(*returnSize)], tmp, cidx * sizeof(int));
(*returnSize)++;
} else {
for (int i = start; i < numsSize; i++) {
tmp[cidx] = nums[i];
backtrack(nums, numsSize, res, returnSize, returnColumnSizes, tmp,
tarSize, cidx + 1, i + 1);
}
}
}

int** subsets(int* nums, int numsSize, int* returnSize, int**


returnColumnSizes) {
int size = pow(2, numsSize);
*returnSize = 0;
*returnColumnSizes = (int*) malloc(size * sizeof(int));
int** res = (int**) malloc(size * sizeof(int*));
int* tmp = (int*) malloc(numsSize * sizeof(int));
for (int i = 0; i <= numsSize; i++) {
backtrack(nums, numsSize, res, returnSize, returnColumnSizes, tmp, i,
0, 0);
}
free(tmp);
return res;
}
5. Word Search

int ROWS;
int COLS;
bool backtrack(char **board, int row, int col, char* word, int index) {
/* Step 1). check the bottom case. */
if (index >= strlen(word))
return true;

/* Step 2). Check the boundaries. */


if (row < 0 || row == ROWS || col < 0 || col == COLS
|| board[row][col] != word[index])
return false;

/* Step 3). explore the neighbors in DFS */


bool ret = false;
// mark the path before the next exploration
board[row][col] = '#';

int rowOffsets[4] = {0, 1, 0, -1};


int colOffsets[4] = {1, 0, -1, 0};
for (int d = 0; d < 4; ++d) {
ret = backtrack(board, row + rowOffsets[d], col + colOffsets[d], word,
index + 1);
if (ret)
break;
}

/* Step 4). clean up and return the result. */


board[row][col] = word[index];
return ret;
}

bool exist(char** board, int boardSize, int* boardColSize, char * word){


ROWS = boardSize;
COLS = *boardColSize;

for (int row = 0; row < ROWS; ++row)


for (int col = 0; col < COLS; ++col)
if (backtrack(board, row, col, word, 0))
return true;
return false;
}
6. Palindrome Partitioning
void dfs(int no_strs, char ***results, char **curr, char * s, int* returnSize,
int** returnColumnSizes){
if(*s == 0){
results[*returnSize] = (char**)malloc(sizeof(char*)*no_strs);
for(int i = 0; i < no_strs; i++)
{
results[*returnSize][i] = (char*)malloc(strlen(curr[i]) + 1);
strcpy(results[*returnSize][i], curr[i]);
}
(*returnColumnSizes)[(*returnSize)++] = no_strs;
return;
}
int len = strlen(s);
for(int end = 0; end < len; end++){
if(isPalindrome(s, end)){
strncpy(curr[no_strs], s, end+1);
curr[no_strs][end+1] = '\0';
dfs(no_strs+1, results, curr, s + end + 1, returnSize,
returnColumnSizes);
}
}
}
char *** partition(char * s, int* returnSize, int** returnColumnSizes){
*returnSize = 0;
if(s == NULL || !strcmp(s, "")) return NULL;

int len = strlen(s) + 1;


*returnColumnSizes = (int*)malloc(sizeof(int)*100000);
char*** results = (char***)malloc(sizeof(char**) * 100000);
char** curr = (char**)malloc(sizeof(char*) * 100000);
for(int i = 0; i<100000; i++)
curr[i] = (char*)malloc(len);
dfs(0, results, curr, s, returnSize, returnColumnSizes);
return results;
}

You might also like