Skip to content

Commit b1b4879

Browse files
authored
Merge branch 'master' into kadane_add_tests
2 parents 371085e + ce9f420 commit b1b4879

30 files changed

+1595
-229
lines changed

DIRECTORY.md

Lines changed: 21 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package com.thealgorithms.backtracking;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* A class to solve a crossword puzzle using backtracking.
8+
* Example:
9+
* Input:
10+
* puzzle = {
11+
* {' ', ' ', ' '},
12+
* {' ', ' ', ' '},
13+
* {' ', ' ', ' '}
14+
* }
15+
* words = List.of("cat", "dog")
16+
*
17+
* Output:
18+
* {
19+
* {'c', 'a', 't'},
20+
* {' ', ' ', ' '},
21+
* {'d', 'o', 'g'}
22+
* }
23+
*/
24+
public final class CrosswordSolver {
25+
private CrosswordSolver() {
26+
}
27+
28+
/**
29+
* Checks if a word can be placed at the specified position in the crossword.
30+
*
31+
* @param puzzle The crossword puzzle represented as a 2D char array.
32+
* @param word The word to be placed.
33+
* @param row The row index where the word might be placed.
34+
* @param col The column index where the word might be placed.
35+
* @param vertical If true, the word is placed vertically; otherwise, horizontally.
36+
* @return true if the word can be placed, false otherwise.
37+
*/
38+
public static boolean isValid(char[][] puzzle, String word, int row, int col, boolean vertical) {
39+
for (int i = 0; i < word.length(); i++) {
40+
if (vertical) {
41+
if (row + i >= puzzle.length || puzzle[row + i][col] != ' ') {
42+
return false;
43+
}
44+
} else {
45+
if (col + i >= puzzle[0].length || puzzle[row][col + i] != ' ') {
46+
return false;
47+
}
48+
}
49+
}
50+
return true;
51+
}
52+
53+
/**
54+
* Places a word at the specified position in the crossword.
55+
*
56+
* @param puzzle The crossword puzzle represented as a 2D char array.
57+
* @param word The word to be placed.
58+
* @param row The row index where the word will be placed.
59+
* @param col The column index where the word will be placed.
60+
* @param vertical If true, the word is placed vertically; otherwise, horizontally.
61+
*/
62+
public static void placeWord(char[][] puzzle, String word, int row, int col, boolean vertical) {
63+
for (int i = 0; i < word.length(); i++) {
64+
if (vertical) {
65+
puzzle[row + i][col] = word.charAt(i);
66+
} else {
67+
puzzle[row][col + i] = word.charAt(i);
68+
}
69+
}
70+
}
71+
72+
/**
73+
* Removes a word from the specified position in the crossword.
74+
*
75+
* @param puzzle The crossword puzzle represented as a 2D char array.
76+
* @param word The word to be removed.
77+
* @param row The row index where the word is placed.
78+
* @param col The column index where the word is placed.
79+
* @param vertical If true, the word was placed vertically; otherwise, horizontally.
80+
*/
81+
public static void removeWord(char[][] puzzle, String word, int row, int col, boolean vertical) {
82+
for (int i = 0; i < word.length(); i++) {
83+
if (vertical) {
84+
puzzle[row + i][col] = ' ';
85+
} else {
86+
puzzle[row][col + i] = ' ';
87+
}
88+
}
89+
}
90+
91+
/**
92+
* Solves the crossword puzzle using backtracking.
93+
*
94+
* @param puzzle The crossword puzzle represented as a 2D char array.
95+
* @param words The list of words to be placed.
96+
* @return true if the crossword is solved, false otherwise.
97+
*/
98+
public static boolean solveCrossword(char[][] puzzle, List<String> words) {
99+
// Create a mutable copy of the words list
100+
List<String> remainingWords = new ArrayList<>(words);
101+
102+
for (int row = 0; row < puzzle.length; row++) {
103+
for (int col = 0; col < puzzle[0].length; col++) {
104+
if (puzzle[row][col] == ' ') {
105+
for (String word : new ArrayList<>(remainingWords)) {
106+
for (boolean vertical : new boolean[] {true, false}) {
107+
if (isValid(puzzle, word, row, col, vertical)) {
108+
placeWord(puzzle, word, row, col, vertical);
109+
remainingWords.remove(word);
110+
if (solveCrossword(puzzle, remainingWords)) {
111+
return true;
112+
}
113+
remainingWords.add(word);
114+
removeWord(puzzle, word, row, col, vertical);
115+
}
116+
}
117+
}
118+
return false;
119+
}
120+
}
121+
}
122+
return true;
123+
}
124+
}
Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,51 @@
11
package com.thealgorithms.backtracking;
22

3-
/*
4-
* Problem Statement :
5-
* Find the number of ways that a given integer, N , can be expressed as the sum of the Xth powers
6-
* of unique, natural numbers. For example, if N=100 and X=3, we have to find all combinations of
7-
* unique cubes adding up to 100. The only solution is 1^3+2^3+3^3+4^3. Therefore output will be 1.
3+
/**
4+
* Problem Statement:
5+
* Find the number of ways that a given integer, N, can be expressed as the sum of the Xth powers
6+
* of unique, natural numbers.
7+
* For example, if N=100 and X=3, we have to find all combinations of unique cubes adding up to 100.
8+
* The only solution is 1^3 + 2^3 + 3^3 + 4^3. Therefore, the output will be 1.
9+
*
10+
* N is represented by the parameter 'targetSum' in the code.
11+
* X is represented by the parameter 'power' in the code.
812
*/
913
public class PowerSum {
1014

11-
private int count = 0;
12-
private int sum = 0;
13-
14-
public int powSum(int n, int x) {
15-
sum(n, x, 1);
16-
return count;
15+
/**
16+
* Calculates the number of ways to express the target sum as a sum of Xth powers of unique natural numbers.
17+
*
18+
* @param targetSum The target sum to achieve (N in the problem statement)
19+
* @param power The power to raise natural numbers to (X in the problem statement)
20+
* @return The number of ways to express the target sum
21+
*/
22+
public int powSum(int targetSum, int power) {
23+
// Special case: when both targetSum and power are zero
24+
if (targetSum == 0 && power == 0) {
25+
return 1; // by convention, one way to sum to zero: use nothing
26+
}
27+
return sumRecursive(targetSum, power, 1, 0);
1728
}
1829

19-
// here i is the natural number which will be raised by X and added in sum.
20-
public void sum(int n, int x, int i) {
21-
// if sum is equal to N that is one of our answer and count is increased.
22-
if (sum == n) {
23-
count++;
24-
return;
25-
} // we will be adding next natural number raised to X only if on adding it in sum the
26-
// result is less than N.
27-
else if (sum + power(i, x) <= n) {
28-
sum += power(i, x);
29-
sum(n, x, i + 1);
30-
// backtracking and removing the number added last since no possible combination is
31-
// there with it.
32-
sum -= power(i, x);
30+
/**
31+
* Recursively calculates the number of ways to express the remaining sum as a sum of Xth powers.
32+
*
33+
* @param remainingSum The remaining sum to achieve
34+
* @param power The power to raise natural numbers to (X in the problem statement)
35+
* @param currentNumber The current natural number being considered
36+
* @param currentSum The current sum of powered numbers
37+
* @return The number of valid combinations
38+
*/
39+
private int sumRecursive(int remainingSum, int power, int currentNumber, int currentSum) {
40+
int newSum = currentSum + (int) Math.pow(currentNumber, power);
41+
42+
if (newSum == remainingSum) {
43+
return 1;
3344
}
34-
if (power(i, x) < n) {
35-
// calling the sum function with next natural number after backtracking if when it is
36-
// raised to X is still less than X.
37-
sum(n, x, i + 1);
45+
if (newSum > remainingSum) {
46+
return 0;
3847
}
39-
}
4048

41-
// creating a separate power function so that it can be used again and again when required.
42-
private int power(int a, int b) {
43-
return (int) Math.pow(a, b);
49+
return sumRecursive(remainingSum, power, currentNumber + 1, newSum) + sumRecursive(remainingSum, power, currentNumber + 1, currentSum);
4450
}
4551
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package com.thealgorithms.backtracking;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* Class to determine if a pattern matches a string using backtracking.
8+
*
9+
* Example:
10+
* Pattern: "abab"
11+
* Input String: "JavaPythonJavaPython"
12+
* Output: true
13+
*
14+
* Pattern: "aaaa"
15+
* Input String: "JavaJavaJavaJava"
16+
* Output: true
17+
*
18+
* Pattern: "aabb"
19+
* Input String: "JavaPythonPythonJava"
20+
* Output: false
21+
*/
22+
public final class WordPatternMatcher {
23+
private WordPatternMatcher() {
24+
}
25+
26+
/**
27+
* Determines if the given pattern matches the input string using backtracking.
28+
*
29+
* @param pattern The pattern to match.
30+
* @param inputString The string to match against the pattern.
31+
* @return True if the pattern matches the string, False otherwise.
32+
*/
33+
public static boolean matchWordPattern(String pattern, String inputString) {
34+
Map<Character, String> patternMap = new HashMap<>();
35+
Map<String, Character> strMap = new HashMap<>();
36+
return backtrack(pattern, inputString, 0, 0, patternMap, strMap);
37+
}
38+
39+
/**
40+
* Backtracking helper function to check if the pattern matches the string.
41+
*
42+
* @param pattern The pattern string.
43+
* @param inputString The string to match against the pattern.
44+
* @param patternIndex Current index in the pattern.
45+
* @param strIndex Current index in the input string.
46+
* @param patternMap Map to store pattern characters to string mappings.
47+
* @param strMap Map to store string to pattern character mappings.
48+
* @return True if the pattern matches, False otherwise.
49+
*/
50+
private static boolean backtrack(String pattern, String inputString, int patternIndex, int strIndex, Map<Character, String> patternMap, Map<String, Character> strMap) {
51+
if (patternIndex == pattern.length() && strIndex == inputString.length()) {
52+
return true;
53+
}
54+
if (patternIndex == pattern.length() || strIndex == inputString.length()) {
55+
return false;
56+
}
57+
58+
char currentChar = pattern.charAt(patternIndex);
59+
if (patternMap.containsKey(currentChar)) {
60+
String mappedStr = patternMap.get(currentChar);
61+
if (inputString.startsWith(mappedStr, strIndex)) {
62+
return backtrack(pattern, inputString, patternIndex + 1, strIndex + mappedStr.length(), patternMap, strMap);
63+
} else {
64+
return false;
65+
}
66+
}
67+
68+
for (int end = strIndex + 1; end <= inputString.length(); end++) {
69+
String substring = inputString.substring(strIndex, end);
70+
if (strMap.containsKey(substring)) {
71+
continue;
72+
}
73+
74+
patternMap.put(currentChar, substring);
75+
strMap.put(substring, currentChar);
76+
if (backtrack(pattern, inputString, patternIndex + 1, end, patternMap, strMap)) {
77+
return true;
78+
}
79+
80+
patternMap.remove(currentChar);
81+
strMap.remove(substring);
82+
}
83+
84+
return false;
85+
}
86+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
/**
4+
* Find the Number Appearing Odd Times in an array
5+
* @author Lakshyajeet Singh Goyal (https://github.com/DarkMatter-999)
6+
*/
7+
8+
public final class NumberAppearingOddTimes {
9+
private NumberAppearingOddTimes() {
10+
}
11+
public static int findOddOccurrence(int[] arr) {
12+
int result = 0;
13+
14+
// XOR all elements in the array
15+
for (int num : arr) {
16+
result ^= num;
17+
}
18+
19+
return result;
20+
}
21+
}

0 commit comments

Comments
 (0)