Skip to content

Palindrome Partitioning Memoization #3

@ChetasShree

Description

@ChetasShree

Hey, this repo you have made has helped me a lot, because I get the logic but when it comes to coding it, it sometimes feels difficult, so thank you for making this amazing repo 🙌🙌.

In this -> https://github.com/shubhamchemate003/Dynamic-Programming-Questions-by-Aditya-Verma/blob/main/pallindrome_partitioning_memoization.cpp

and in this too -> https://github.com/shubhamchemate003/Dynamic-Programming-Questions-by-Aditya-Verma/blob/main/pallindrome_partitioning_memoized_optimization.cpp

just pass the string with reference(&) in both the function ( isPallindrome and Solve) and it will not give TLE .
Just wanted to help, because I think there would be a lot of people like me who are getting help through this repo.

code :

bool isPallindrome(string &X, int i, int j) {
	while (i <= j) {
		if (X[i] != X[j])
			return false;
		i++, j--;
	}

	return true;
}

int Solve(string &X, int i, int j) {
	if (i >= j || isPallindrome(X, i, j)) {
		t[i][j] = 0;
		return 0;
	}

	if (t[i][j] != -1)
		return t[i][j];

	int ans = INT_MAX;
	for (int k = i; k < j; k++) {
		int temp_ans = Solve(X, i, k) + Solve(X, k + 1, j) + 1;
		ans = min(ans, temp_ans);
	}

	return t[i][j] = ans;
} 

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions