Skip to content

Commit

Permalink
Added KMP algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
neerazz committed Feb 11, 2024
1 parent 85c6e37 commit 60419a3
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 0 deletions.
41 changes: 41 additions & 0 deletions Algorithms/dynamic-problem/CherryPickupII.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* Created on: Jun 21, 2020
* Questions: https://leetcode.com/problems/cherry-pickup/
*/
public class CherryPickupII {

public static void main(String[] args) {
System.out.println(cherryPickup(new int[][]{{3,1,1}, {2,5,1}, {1,5,5},{2,1,1}}) + "= 24");

System.out.println(cherryPickup(new int[][]{{1, 1, -1}, {1, -1, 1}, {-1, 1, 1}}) + "= 4");
System.out.println(cherryPickup(new int[][]{{1}}) + "= 1");
System.out.println(cherryPickup(new int[][]{{1, 1, 1, 1, 0, 0, 0}, {0, 0, 0, 1, 0, 0, 0}, {0, 0, 0, 1, 0, 0, 1}, {1, 0, 0, 1, 0, 0, 0}, {0, 0, 0, 1, 0, 0, 0}, {0, 0, 0, 1, 0, 0, 0}, {0, 0, 0, 1, 1, 1, 1}}) +
"= 7");
}

public static int cherryPickup(int[][] grid) {
int rows = grid.length;
int cols = rows > 0 ? grid[0].length : 0;
Integer[][][] dp = new Integer[rows][cols][cols];
return helper(grid, 0, 0, cols - 1, dp, rows, cols);
}

private static int helper(int[][] grid, int row, int c1, int c2, Integer[][][] dp, int rows, int cols) {
if (row == rows) return 0;
if (c1 < 0 || c1 >= cols || c2 < 0 || c2 >= cols) return Integer.MIN_VALUE;
if (dp[row][c1][c2] != null) {
return dp[row][c1][c2];
}
int[] dirs = {-1, 0, 1};
int max = 0;
for (int d1 : dirs) {
for (int d2 : dirs) {
int next = helper(grid, row + 1, c1 + d1, c2 + d2, dp, rows, cols);
max = Math.max(max, next);
}
}
max += c1 == c2 ? grid[row][c1] : grid[row][c1] + grid[row][c2];
dp[row][c1][c2] = max;
return max;
}
}
62 changes: 62 additions & 0 deletions Algorithms/kmp/FindPatternInString.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import java.util.*;
import java.io.*;

/**
* Created on: Feb 11, 2024
* Ref:
*/

public class FindPatternInString {

public static void main(String[] args) {
System.out.println(getPatternCount("abcaeabc", "abc"));
}

private static int getPatternCount(String str, String pattern) {
int count = 0, len = str.length(), p = pattern.length();
// Build lps.
int[] lps = new int[p];
buildLPS(pattern, lps);
int i = 0, j = 0;
while (i < len) {
if (str.charAt(i) == pattern.charAt(j)) {
i++;
j++;
if (j == p) {
j = lps[j - 1];
}
} else {
if (j > 0) {
// Find the previous prefix and suffix.
j = lps[j - 1];
} else {
// If there is no any previous prefix then
i++;
}
}
}
return count;
}

private static void buildLPS(String pattern, int[] lps) {
int len = lps.length, pre = 0;
lps[0] = 0;
int i = 1;
while (i < len) {
if (pattern.charAt(i) == pattern.charAt(pre)) {
pre++;
lps[i] = pre;
i++;
} else {
if (pre != 0) {
pre = lps[pre - 1];
} else {
lps[i] = pre;
i++;
}
}
}
}


}

0 comments on commit 60419a3

Please sign in to comment.