Can someone explain the mistake in this code? Leetcode 44 Wildcard Matching - recursion

Can Someone explain what is wrong in this code ?
It is failing on testcase s = "aa" and p = "*".
I have followed recursion and dynamic programming code here
Leetcode 44 Wildcard Matching
class Solution {
public boolean isMatch(String s, String p) {
int n = s.length();
int m = p.length();
int[][] dp = new int[n][m];
for(int[] it : dp)
Arrays.fill(it, -1);
return solve(n-1 , m-1, s ,p , dp);
}
public boolean solve(int i, int j, String s, String p, int[][] dp){
if(i < 0 && j < 0) return true;
if(i < 0 && j >=0){
while(j>=0){
if(p.charAt(j) + "" == "*") j--;
else return false;
}
return true;
}
if(j < 0 && i >=0) return false;
if(dp[i][j] != -1){
if(dp[i][j]==1) return true;
return false;
}
if(s.charAt(i) == p.charAt(j) || p.charAt(j) + "" == "?"){
boolean temp = solve(i-1,j-1,s,p,dp);
if(temp == false) dp[i][j] = 0;
else
dp[i][j] = 1;
return temp;
}
if(p.charAt(j) + "" == "*"){
boolean temp = solve(i-1,j,s,p,dp) || solve(i,j-1,s,p,dp);
if(temp == false)
dp[i][j] = 0;
else
dp[i][j] = 1;
return temp;
}
dp[i][j] = 0;
return false;
}
}

Related

trying to run a 3axes gantry in sequence mode similar to the sequence of serial data sent

i want to run my 3 axes gantry(stm32) in sequence similar to the sequence of serial data sent from the pc but this is not happening. no data is lost as all the axes perform their task but in wrong sequence.
this is the recieve complete callback loop
`void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if(r != ',')
{
Rx[i++] = r;
}
else
{
strncpy(s1,Rx,i);
Home = strrchr(s1, 'H');
StringX = strrchr(s1, 'X');
StringY = strrchr(s1, 'Y');
StringZ = strrchr(s1, 'Z');
StringW = strrchr(s1, 'W');
if(Home[0] == 'H' && Home[1] == 'O' && Home[2] == 'M' && Home[3] == 'E')
{
homeValue = 1;
}
if(StringX[0] == 'X')
{
MoveX= substr(StringX,1,8);
MoveXvalue = (atol(MoveX))/100;
SpeedX = substr(StringX,8,12);
SpeedXvalue = atol(SpeedX);
insertintoQueue_X(MoveXvalue, SpeedXvalue);
insertintoSequenceQueue (1);
}
if(StringY[0] == 'Y')
{
MoveY= substr(StringY,1,8);
MoveYvalue = (atol(MoveY))/100;
SpeedY = substr(StringY,8,12);
SpeedYvalue = atol(SpeedY);
insertintoQueue_Y(MoveYvalue, SpeedYvalue);
insertintoSequenceQueue (2);
///goes on for other axes
/
i = 0;
}
}`
i tried sequencing these received cmds with below loop(posting only for one axis)
`void insertintoQueue_X (int j, int l)
{
uint8_t queuefull[] = "XFULL";
if(countXmove == 256 || countXspeed == 256 )
{
HAL_UART_Transmit_IT (&huart1, queuefull, sizeof (queuefull));
}
Xmovequeue[countXmove] = j ;
countXmove++;
Xspeedqueue[countXspeed] = l;
countXspeed++;
}
void removefromQueue_X()
{
//uint8_t queueEmpty[] = "XEMPTY";
/*if(countXmove == 0 || countXspeed == 0)
{
HAL_UART_Transmit_IT (&huart2, queueEmpty, sizeof (queueEmpty));
}*/
currentXmoveelement = Xmovequeue[0];
currentXspeedelement = Xspeedqueue[0];
for(int i = 0; i < countXmove - 1; i++)
{
Xmovequeue[i] = Xmovequeue[i+1];
Xspeedqueue[i] = Xspeedqueue[i+1];
}
}`
while loop is as below
` removefromSequenceQueue();//only called once in while loop
for(int p = 0; p < countXmove ; p++)
{
if(currentSequence == 1)
{
removefromQueue_X();
if(currentXmoveelement >0)
{
//uint8_t Xmovvestatus[] = "XMMM";
if(CurrentXposition != currentXmoveelement)
{
if(CurrentXposition > currentXmoveelement )
{
currentmoveX = CurrentXposition - currentXmoveelement;
motorMoveTo(currentmoveX,0,currentXspeedelement,20,SMSPR_X,SMS_X,SMDPR_X,SMD_X);
}
else if(CurrentXposition < currentXmoveelement )
{
// HAL_UART_Transmit_IT (&huart1, Xmovvestatus, sizeof (Xmovvestatus));
currentmoveX = currentXmoveelement - CurrentXposition ;
motorMoveTo(currentmoveX,1,currentXspeedelement,20,SMSPR_X,SMS_X,SMDPR_X,SMD_X);
}
CurrentXposition = currentXmoveelement;
currentXmoveelement = 0;
currentmoveX = 0;
MoveXvalue=0;
}
}
}
}

Why do I get a StackOverFlow error on the first recursive call?

Below is the code I am referring to, and the first recursive call # checkDirections(grid, i - 1, j) is giving me a StackOverFlow error. I understand that this means the code is not hitting the base case, but I do not understand why.
class Solution {
public int orangesRotting(int[][] grid) {
int rowLength = grid.length;
int colLength = grid[0].length;
int minMinutes = 0;
for (int i = 0; i < rowLength; i++) {
for (int j = 0; j < colLength; j++) {
if (grid[i][j] == 2) {
checkDirections(grid, i, j);
}
}
}
return minMinutes;
}
public void checkDirections(int[][] grid, int i, int j) {
if ((i < 0 || i > grid.length || j < 0 || j > grid[0].length) || grid[i][j] == 0) {
return;
} else if (grid[i][j] == 1) {
grid[i][j] = 2;
return;
}
//check left
checkDirections(grid, i - 1, j);
//check right
checkDirections(grid, i + 1, j);
//check up
checkDirections(grid, i, j - 1);
//check down
checkDirections(grid, i, j + 1);
}
}

What is causing the buffer overrun error in below code

Unable to figure out what is causing the buffer overflow in below code. I reckon it has to do with the vector but I am guarding against out of bounds access. Is there anything else that could be causing the overflow?
class Solution {
public:
bool canPartition(vector<int>& nums) {
int sum = 0;
bool canPartition = true;
vector<vector<int>> dp(nums.size(), vector<int>(sum / 2 + 1, -1));
sum = accumulate(nums.begin(),nums.end(),0);
if (sum % 2 != 0)
{
canPartition = false;
}
if (true == canPartition)
{
canPartition = canPartitionRecursive(nums, 0, sum/2, dp);
}
return canPartition;
}
bool canPartitionRecursive(vector<int>& nums, int index, int sum,
vector<vector<int>>& dp)
{
if (sum == 0)
{
return true;
}
if (index >= nums.size() || sum < 0)
{
return false;
}
if (dp[index][sum] != -1)
{
if (true == canPartitionRecursive(nums, index+1, sum - nums[index],dp))
{
dp[index][sum] = 1;
return true;
}
dp[index][sum] = canPartitionRecursive(nums, index + 1, sum, dp);
}
return dp[index][sum] = 1? true:false;
}
};
This looks like a transpositional error (all subvector will have size 1):
int sum = 0;
vector<vector<int>> dp(nums.size(), vector<int>(sum / 2 + 1, -1));
sum = accumulate(nums.begin(),nums.end(),0);
Perhaps calculation of sum should be moved before dp initialization?

Check if a number is prime using recursion

This is a recursive check if this is a prime number -- is it correct?
public static boolean isPrimeRecursive (int n,int i){//i eqoual to n
if (n <= 1) {
return false;
}if (i==1){
return false;
}if(n%i==0){
return false;
}
return isPrimeRecursive(n,i--);
}
I wouldn't burden your user with that mysterious second argument but rather present a different method of just one argument, that first deals with numbers less than 2 and even numbers, and then calls into your recursive method with the proper arguments:
private static boolean isPrimeRecursive(int n, int i) {
if (i * i > n) {
return true;
}
if (n % i == 0) {
return false;
}
return isPrimeRecursive(n, i + 2);
}
public static boolean isPrime(int n) {
if (n <= 2 || n % 2 == 0) {
return (n == 2);
}
return isPrimeRecursive(n, 3);
}
public static void main(String[] args) {
System.out.println(isPrime(Integer.parseInt(args[0])));
}
With your code, you should start of i with a value of n-1 since n % n is always true of prime numbers.
Then in your condition (if (i == 1) { ... }, should return true because if the method reaches to 1, then it fulfills all other conditions.
Finally in your return statement return isPrimeRecursive(n, i++);, it is better to use ++i since i++ will increment after the execution of the function with the value of i.
public static boolean isPrimeRecursive (int n,int i){
if (n <= 1) {
return false;
}
if (i == 1) {
return true;
}
if(n % i == 0){
return false;
}
return isPrimeRecursive(n, --i);
}
In your main function, you will then use:
int n = 17;
System.out.println(isPrimeRecursive(n, n-1);
Another way of doing it is to always start i with a value of 2 and increment it's value. From there.
public static boolean isPrimeRecursive (int n, int i) {
if (n <= 2) {
return (n == 2) ? true : false;
}
if (i >= n) {
return true;
}
if (n % i == 0) {
return false;
}
return isPrimeRecursive(n, ++i);
}
Then you simple do:
int n = 17;
System.out.println(isPrimeRecursive(n, 2);

Computing multiple numbers with multiple operations

So I am creating a calculator that will compute numbers.I have this code(I will not include very basic codes)
long num1, num2, answer;
boolean mySwitch = false;
boolean do_subtraction_flag = false; // when true we will apply subtraction
boolean multiply = false;
boolean divide = false;
void loop()
{
char keypressed = myKeypad.getKey();
if(keypressed != NO_KEY)
{
Serial.print(keypressed);
if(keypressed > 47 && keypressed < 58) // is between '0' and '9'
{
if(!mySwitch)
{
num1 = (num1 * 10) + (keypressed - 48);
}
else
{
num2 = (num2 * 10) + (keypressed - 48);
}
}
if(keypressed == '=')
{
if(do_subtraction_flag) // we want to subtract the numbers
{
answer = num1 - num2;
}else if(multiply){
answer = num1 * num2;
}else if(divide){
answer = num1 / num2;
}
else // we want to add the numbers
{
answer = num1 + num2;
}
Serial.println(answer);
num1 = 0;
num2 = 0;
mySwitch = false;
do_subtraction_flag = false;
multiply = false;
divide = false;
}
else if(keypressed == '+')
{
mySwitch = true;
}
else if(keypressed == '*'){
mySwitch = true;
multiply = true;
}else if(keypressed == '/'){
mySwitch = true;
divide = true;
}
else if(keypressed == '-')
{
mySwitch = true;
do_subtraction_flag = true;
}else if (keypressed == 'C'){
for (int i=0; i < 80; i++)
{
Serial.write(8); // print 80 times backspace (BS)
}
}
}
}
Im confused here because I want to compute multiple number with multiple operands (eg., 2+1+3 or 2+1-2), but when i add another variable 'num3' what should I suppose to do with it? if i put it into the do_subtraction flag what if the user inputs 2-1+3?Is it possible to compute 3 numbers with this code?I'm getting confused here but let me know if you are also confused what I want to do
Make an array to hold the numbers and operators that you input, then loop through them when the input == '='. Have your num1 always hold the result and num2 hold the next array value.

Resources