Skip to content

Commit

Permalink
🎨 Format files (🛠️ from Github Actions)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahmad-A0 committed Sep 11, 2022
1 parent 5c2b18e commit fd08e46
Show file tree
Hide file tree
Showing 142 changed files with 2,163 additions and 1,991 deletions.
41 changes: 20 additions & 21 deletions java/10-Regular-Expression-Matching.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
class Solution {

public boolean isMatch(String s, String p) {
boolean[][] cache = new boolean[s.length() + 1][p.length() + 1];

boolean[][] cache = new boolean[s.length() + 1][p.length() + 1];

return dfs(cache , s, p, 0 , 0);
}
return dfs(cache, s, p, 0, 0);
}

private boolean dfs(boolean[][] cache, String s, String p , int i, int j) {

if(cache[i][j] != false)
return cache[i][j];
private boolean dfs(boolean[][] cache, String s, String p, int i, int j) {
if (cache[i][j] != false) return cache[i][j];

if( i >= s.length() && j>= p.length())
return true;
if (i >= s.length() && j >= p.length()) return true;

if(j >= p.length())
return false;
if (j >= p.length()) return false;

boolean match = i < s.length() && (s.charAt(i) == p.charAt(j) || p.charAt(j) == '.');
boolean match =
i < s.length() &&
(s.charAt(i) == p.charAt(j) || p.charAt(j) == '.');

if(j + 1 < p.length() && p.charAt(j+1)== '*' ) {
cache[i][j] = dfs(cache, s, p, i, j+2) || (match && dfs(cache, s, p, i+1, j));
} else {
cache[i][j] = match && dfs(cache, s, p, i+1, j+1);
}
if (j + 1 < p.length() && p.charAt(j + 1) == '*') {
cache[i][j] =
dfs(cache, s, p, i, j + 2) ||
(match && dfs(cache, s, p, i + 1, j));
} else {
cache[i][j] = match && dfs(cache, s, p, i + 1, j + 1);
}

return cache[i][j];
}
}
return cache[i][j];
}
}
5 changes: 1 addition & 4 deletions java/1143-Longest-Common-Subsequence.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public int LCS(String s1, String s2, int i, int j, int[][] dp) {
}
}


// Iterative version
class Solution {

public int longestCommonSubsequence(String text1, String text2) {
//O(n*m)/O(n*m) time/memory
if (text1.isEmpty() || text2.isEmpty()) {
Expand All @@ -43,15 +43,12 @@ public int longestCommonSubsequence(String text1, String text2) {
for (int j = 1; j <= text2.length(); j++) {
if (text1.charAt(i - 1) == text2.charAt(j - 1)) {
dp[i][j] = 1 + dp[i - 1][j - 1];

} else {
dp[i][j] = Math.max(dp[i][j - 1], dp[i - 1][j]);
}

}
}

return dp[text1.length()][text2.length()];
}
}

43 changes: 20 additions & 23 deletions java/115-Distinct-Subsequences.java
Original file line number Diff line number Diff line change
@@ -1,43 +1,40 @@
// Dynammic Programming - Memoization
// Time Complexity O(s * t) | Space Complexity O(s * t)
class Solution {

public int numDistinct(String s, String t) {

int n = s.length() + 1;
int m = t.length() + 1;
int[][] memo = new int[n][m];
for (int[] row : memo){
Arrays.fill(row, -1);

for (int[] row : memo) {
Arrays.fill(row, -1);
}

return recursion(s, t, 0, 0, memo);

}


public int recursion(String s, String t, int sIdx, int tIdx, int[][] memo){

if (memo[sIdx][tIdx] != -1){

public int recursion(String s, String t, int sIdx, int tIdx, int[][] memo) {
if (memo[sIdx][tIdx] != -1) {
return memo[sIdx][tIdx];
}
if (tIdx >= t.length()){

if (tIdx >= t.length()) {
return 1;
}
if (sIdx >= s.length()){

if (sIdx >= s.length()) {
return 0;
}

if (t.charAt(tIdx) == s.charAt(sIdx)){
memo[sIdx][tIdx] = recursion(s, t, sIdx + 1, tIdx + 1, memo) + recursion(s, t, sIdx + 1, tIdx, memo);
return memo[sIdx][tIdx];

if (t.charAt(tIdx) == s.charAt(sIdx)) {
memo[sIdx][tIdx] =
recursion(s, t, sIdx + 1, tIdx + 1, memo) +
recursion(s, t, sIdx + 1, tIdx, memo);
return memo[sIdx][tIdx];
}

memo[sIdx][tIdx] = recursion(s, t, sIdx + 1, tIdx, memo);
return memo[sIdx][tIdx];

}

}
}
24 changes: 12 additions & 12 deletions java/121-Best-Time-to-Buy-and-Sell-Stock.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
class Solution {

public int maxProfit(int[] prices) {
int left = 0;
int right = 1;
int maxProfit = 0;
while (right < prices.length) {
if (prices[left] < prices[right]) {
maxProfit = Math.max(maxProfit, prices[right] - prices[left]);
right++;
} else {
left = right;
right++;
int left = 0;
int right = 1;
int maxProfit = 0;
while (right < prices.length) {
if (prices[left] < prices[right]) {
maxProfit = Math.max(maxProfit, prices[right] - prices[left]);
right++;
} else {
left = right;
right++;
}
}
}
return maxProfit;
return maxProfit;
}
}
6 changes: 3 additions & 3 deletions java/124-Binary-Tree-Maximum-Path-Sum.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@
* }
*/
class Solution {

public int maxPathSum(TreeNode root) {
int[] res = { Integer.MIN_VALUE };
maxPathSum(root, res);
return res[0];
}

public int maxPathSum(TreeNode root, int[] res) {
if (root == null)
return 0;
if (root == null) return 0;

int left = Math.max(0, maxPathSum(root.left, res));
int right = Math.max(0, maxPathSum(root.right, res));
res[0] = Math.max(res[0], root.val + left + right);

return root.val + Math.max(left, right);
}
}
}
49 changes: 29 additions & 20 deletions java/127-Word-Ladder.java
Original file line number Diff line number Diff line change
@@ -1,48 +1,57 @@
package java;

class Solution {
public int ladderLength(String beginWord, String endWord, List<String> wordList) {

public int ladderLength(
String beginWord,
String endWord,
List<String> wordList
) {
Map<String, List<String>> adjlist = new HashMap<>();
wordList.add(beginWord);
for(String word:wordList){
StringBuilder string=null;
for(int i=0;i<word.length();i++){
string = new StringBuilder(word);
string.setCharAt(i, '*');
List<String> wordlist = adjlist.getOrDefault(string.toString(), new ArrayList<String>());
wordlist.add(word);
adjlist.put(string.toString(), wordlist);
for (String word : wordList) {
StringBuilder string = null;
for (int i = 0; i < word.length(); i++) {
string = new StringBuilder(word);
string.setCharAt(i, '*');
List<String> wordlist = adjlist.getOrDefault(
string.toString(),
new ArrayList<String>()
);
wordlist.add(word);
adjlist.put(string.toString(), wordlist);
}
}

Queue<String> queue = new LinkedList<>();
queue.offer(beginWord);
Set<String> visited = new HashSet<>();
visited.add(beginWord);
int step = 1;
StringBuilder string=null;
while(!queue.isEmpty()){
step++;
StringBuilder string = null;
while (!queue.isEmpty()) {
step++;
int size = queue.size();
for(int j=0;j<size;j++){
for (int j = 0; j < size; j++) {
String str = queue.poll();

for(int i=0;i<str.length();i++){
for (int i = 0; i < str.length(); i++) {
string = new StringBuilder(str);
string.setCharAt(i, '*');
for(String pat:adjlist.get(string.toString())){
if(pat.equals(endWord)){
for (String pat : adjlist.get(string.toString())) {
if (pat.equals(endWord)) {
return step;
}
if(visited.contains(pat)){
if (visited.contains(pat)) {
continue;
}
queue.offer(pat);
visited.add(pat);
}
}
}
}
// step++;
}
return 0;
}
}
}
35 changes: 18 additions & 17 deletions java/1584-Min-Cost-to-Connect-All-Points.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,36 @@
class Solution {

// Time Complexity: O(N^2 log(N)) where N is the length of points. N^2 comes from the fact we need to find the distance between a currNode and every other node to pick the shortest distance. log(N) comes from Priority Queue
// Space Complexity: O(N^2)
public int minCostConnectPoints(int[][] points) {
PriorityQueue<int[]> pq = new PriorityQueue<>((a,b) -> a[0] - b[0]); // edge weight, the index of next node
pq.offer(new int[]{0,0});
PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> a[0] - b[0]); // edge weight, the index of next node
pq.offer(new int[] { 0, 0 });
int len = points.length;
Set<Integer> visited = new HashSet<>();
int cost = 0;
// When visited.size() == points.len meaning that all the nodes has been connected.
while(visited.size() < len){

// When visited.size() == points.len meaning that all the nodes has been connected.
while (visited.size() < len) {
int[] arr = pq.poll();

int weight = arr[0];
int currNode = arr[1];

if(visited.contains(currNode))
continue;


if (visited.contains(currNode)) continue;

visited.add(currNode);
cost += weight;

for(int nextNode = 0; nextNode < len; nextNode++){
if(!visited.contains(nextNode)){
int nextWeight = Math.abs(points[nextNode][0] - points[currNode][0]) + Math.abs(points[nextNode][1] - points[currNode][1]);
pq.offer(new int[]{nextWeight, nextNode});

for (int nextNode = 0; nextNode < len; nextNode++) {
if (!visited.contains(nextNode)) {
int nextWeight =
Math.abs(points[nextNode][0] - points[currNode][0]) +
Math.abs(points[nextNode][1] - points[currNode][1]);
pq.offer(new int[] { nextWeight, nextNode });
}
}
}



return cost;
}
}
Loading

0 comments on commit fd08e46

Please sign in to comment.