mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-09 20:20:56 +08:00
Add multiple algorithms (#2442)
This commit is contained in:
183
Others/Implementing_auto_completing_features_using_trie.java
Normal file
183
Others/Implementing_auto_completing_features_using_trie.java
Normal file
@ -0,0 +1,183 @@
|
||||
package Others;
|
||||
|
||||
|
||||
// Java Program to implement Auto-Complete
|
||||
// Feature using Trie
|
||||
|
||||
import java.util.*;
|
||||
import java.io.*;
|
||||
import java.lang.*;
|
||||
|
||||
class Trieac {
|
||||
|
||||
// Alphabet size (# of symbols)
|
||||
public static final int ALPHABET_SIZE = 26;
|
||||
|
||||
// Trie node
|
||||
static class TrieNode
|
||||
{
|
||||
TrieNode children[] = new TrieNode[ALPHABET_SIZE];
|
||||
|
||||
// isWordEnd is true if the node represents
|
||||
// end of a word
|
||||
boolean isWordEnd;
|
||||
};
|
||||
|
||||
// Returns new trie node (initialized to NULLs)
|
||||
static TrieNode getNode() {
|
||||
TrieNode pNode = new TrieNode();
|
||||
pNode.isWordEnd = false;
|
||||
|
||||
for(int i = 0; i < ALPHABET_SIZE; i++)
|
||||
pNode.children[i] = null;
|
||||
|
||||
return pNode;
|
||||
}
|
||||
|
||||
// If not present, inserts key into trie. If the
|
||||
// key is prefix of trie node, just marks leaf node
|
||||
static void insert(TrieNode root, final String key)
|
||||
{
|
||||
TrieNode pCrawl = root;
|
||||
|
||||
for(int level = 0; level < key.length(); level++)
|
||||
{
|
||||
int index = (key.charAt(level) - 'a');
|
||||
if (pCrawl.children[index] == null)
|
||||
pCrawl.children[index] = getNode();
|
||||
pCrawl = pCrawl.children[index];
|
||||
}
|
||||
|
||||
// mark last node as leaf
|
||||
pCrawl.isWordEnd = true;
|
||||
}
|
||||
|
||||
// Returns true if key presents in trie, else false
|
||||
boolean search(TrieNode root, final String key)
|
||||
{
|
||||
int length = key.length();
|
||||
TrieNode pCrawl = root;
|
||||
|
||||
for (int level = 0; level < length; level++)
|
||||
{
|
||||
int index = (key.charAt(level) - 'a');
|
||||
|
||||
if (pCrawl.children[index] == null)
|
||||
pCrawl = pCrawl.children[index];
|
||||
}
|
||||
|
||||
return (pCrawl != null && pCrawl.isWordEnd);
|
||||
}
|
||||
|
||||
// Returns 0 if current node has a child
|
||||
// If all children are NULL, return 1.
|
||||
static boolean isLastNode(TrieNode root)
|
||||
{
|
||||
for (int i = 0; i < ALPHABET_SIZE; i++)
|
||||
if (root.children[i] != null)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
// Recursive function to print auto-suggestions
|
||||
// for given node.
|
||||
static void suggestionsRec(TrieNode root, String currPrefix)
|
||||
{
|
||||
// found a string in Trie with the given prefix
|
||||
if (root.isWordEnd)
|
||||
{
|
||||
System.out.println(currPrefix);
|
||||
}
|
||||
|
||||
// All children struct node pointers are NULL
|
||||
if (isLastNode(root))
|
||||
return;
|
||||
|
||||
for (int i = 0; i < ALPHABET_SIZE; i++)
|
||||
{
|
||||
if (root.children[i] != null)
|
||||
{
|
||||
// append current character to currPrefix string
|
||||
currPrefix += (char)(97 + i);
|
||||
|
||||
// recur over the rest
|
||||
suggestionsRec(root.children[i], currPrefix);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Fucntion to print suggestions for
|
||||
// given query prefix.
|
||||
static int printAutoSuggestions(TrieNode root,
|
||||
final String query)
|
||||
{
|
||||
TrieNode pCrawl = root;
|
||||
|
||||
// Check if prefix is present and find the
|
||||
// the node (of last level) with last character
|
||||
// of given string.
|
||||
int level;
|
||||
int n = query.length();
|
||||
|
||||
for (level = 0; level < n; level++)
|
||||
{
|
||||
int index = (query.charAt(level) - 'a');
|
||||
|
||||
// no string in the Trie has this prefix
|
||||
if (pCrawl.children[index] == null)
|
||||
return 0;
|
||||
|
||||
pCrawl = pCrawl.children[index];
|
||||
}
|
||||
|
||||
// If prefix is present as a word.
|
||||
boolean isWord = (pCrawl.isWordEnd == true);
|
||||
|
||||
// If prefix is last node of tree (has no
|
||||
// children)
|
||||
boolean isLast = isLastNode(pCrawl);
|
||||
|
||||
// If prefix is present as a word, but
|
||||
// there is no subtree below the last
|
||||
// matching node.
|
||||
if (isWord && isLast)
|
||||
{
|
||||
System.out.println(query);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// If there are are nodes below last
|
||||
// matching character.
|
||||
if (!isLast)
|
||||
{
|
||||
String prefix = query;
|
||||
suggestionsRec(pCrawl, prefix);
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Driver code
|
||||
public static void main(String[] args)
|
||||
{
|
||||
TrieNode root = getNode();
|
||||
insert(root, "hello");
|
||||
insert(root, "dog");
|
||||
insert(root, "hell");
|
||||
insert(root, "cat");
|
||||
insert(root, "a");
|
||||
insert(root, "hel");
|
||||
insert(root, "help");
|
||||
insert(root, "helps");
|
||||
insert(root, "helping");
|
||||
int comp = printAutoSuggestions(root, "hel");
|
||||
|
||||
if (comp == -1)
|
||||
System.out.println("No other strings found "+
|
||||
"with this prefix\n");
|
||||
else if (comp == 0)
|
||||
System.out.println("No string found with"+
|
||||
" this prefix\n");
|
||||
}
|
||||
}
|
159
Others/Sudoku.java
Normal file
159
Others/Sudoku.java
Normal file
@ -0,0 +1,159 @@
|
||||
package Others;
|
||||
|
||||
|
||||
class Sudoku
|
||||
{
|
||||
public static boolean isSafe(int[][] board,
|
||||
int row, int col,
|
||||
int num)
|
||||
{
|
||||
// Row has the unique (row-clash)
|
||||
for (int d = 0; d < board.length; d++)
|
||||
{
|
||||
|
||||
// Check if the number we are trying to
|
||||
// place is already present in
|
||||
// that row, return false;
|
||||
if (board[row][d] == num) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Column has the unique numbers (column-clash)
|
||||
for (int r = 0; r < board.length; r++)
|
||||
{
|
||||
|
||||
// Check if the number
|
||||
// we are trying to
|
||||
// place is already present in
|
||||
// that column, return false;
|
||||
if (board[r][col] == num)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Corresponding square has
|
||||
// unique number (box-clash)
|
||||
int sqrt = (int)Math.sqrt(board.length);
|
||||
int boxRowStart = row - row % sqrt;
|
||||
int boxColStart = col - col % sqrt;
|
||||
|
||||
for (int r = boxRowStart;
|
||||
r < boxRowStart + sqrt; r++)
|
||||
{
|
||||
for (int d = boxColStart;
|
||||
d < boxColStart + sqrt; d++)
|
||||
{
|
||||
if (board[r][d] == num)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// if there is no clash, it's safe
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean solveSudoku(
|
||||
int[][] board, int n)
|
||||
{
|
||||
int row = -1;
|
||||
int col = -1;
|
||||
boolean isEmpty = true;
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
if (board[i][j] == 0)
|
||||
{
|
||||
row = i;
|
||||
col = j;
|
||||
|
||||
// We still have some remaining
|
||||
// missing values in Sudoku
|
||||
isEmpty = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!isEmpty) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// No empty space left
|
||||
if (isEmpty)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
// Else for each-row backtrack
|
||||
for (int num = 1; num <= n; num++)
|
||||
{
|
||||
if (isSafe(board, row, col, num))
|
||||
{
|
||||
board[row][col] = num;
|
||||
if (solveSudoku(board, n))
|
||||
{
|
||||
// print(board, n);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
// replace it
|
||||
board[row][col] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static void print(
|
||||
int[][] board, int N)
|
||||
{
|
||||
|
||||
// We got the answer, just print it
|
||||
for (int r = 0; r < N; r++)
|
||||
{
|
||||
for (int d = 0; d < N; d++)
|
||||
{
|
||||
System.out.print(board[r][d]);
|
||||
System.out.print(" ");
|
||||
}
|
||||
System.out.print("\n");
|
||||
|
||||
if ((r + 1) % (int)Math.sqrt(N) == 0)
|
||||
{
|
||||
System.out.print("");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Driver Code
|
||||
public static void main(String args[])
|
||||
{
|
||||
|
||||
int[][] board = new int[][] {
|
||||
{ 3, 0, 6, 5, 0, 8, 4, 0, 0 },
|
||||
{ 5, 2, 0, 0, 0, 0, 0, 0, 0 },
|
||||
{ 0, 8, 7, 0, 0, 0, 0, 3, 1 },
|
||||
{ 0, 0, 3, 0, 1, 0, 0, 8, 0 },
|
||||
{ 9, 0, 0, 8, 6, 3, 0, 0, 5 },
|
||||
{ 0, 5, 0, 0, 9, 0, 6, 0, 0 },
|
||||
{ 1, 3, 0, 0, 0, 0, 2, 5, 0 },
|
||||
{ 0, 0, 0, 0, 0, 0, 0, 7, 4 },
|
||||
{ 0, 0, 5, 2, 0, 6, 3, 0, 0 }
|
||||
};
|
||||
int N = board.length;
|
||||
|
||||
if (solveSudoku(board, N))
|
||||
{
|
||||
// print solution
|
||||
print(board, N);
|
||||
}
|
||||
else {
|
||||
System.out.println("No solution");
|
||||
}
|
||||
}
|
||||
}
|
66
Strings/List_all_Possible_Words_From_Phone_Digits.java
Normal file
66
Strings/List_all_Possible_Words_From_Phone_Digits.java
Normal file
@ -0,0 +1,66 @@
|
||||
package Strings;
|
||||
import java.util.*;
|
||||
import java.lang.*;
|
||||
import java.io.*;
|
||||
|
||||
public class List_all_Possible_Words_From_Phone_Digits {
|
||||
|
||||
static Character[][] numberToCharMap;
|
||||
|
||||
private static List<String> printWords(int[] numbers,
|
||||
int len,
|
||||
int numIndex,
|
||||
String s)
|
||||
{
|
||||
if(len == numIndex)
|
||||
{
|
||||
return new ArrayList<>(Collections.singleton(s));
|
||||
}
|
||||
|
||||
List<String> stringList = new ArrayList<>();
|
||||
|
||||
for(int i = 0;
|
||||
i < numberToCharMap[numbers[numIndex]].length; i++)
|
||||
{
|
||||
String sCopy =
|
||||
String.copyValueOf(s.toCharArray());
|
||||
sCopy = sCopy.concat(
|
||||
numberToCharMap[numbers[numIndex]][i].toString());
|
||||
stringList.addAll(printWords(numbers, len,
|
||||
numIndex + 1,
|
||||
sCopy));
|
||||
}
|
||||
return stringList;
|
||||
}
|
||||
|
||||
private static void printWords(int[] numbers)
|
||||
{
|
||||
generateNumberToCharMap();
|
||||
List<String> stringList =
|
||||
printWords(numbers, numbers.length, 0, "");
|
||||
stringList.stream().forEach(System.out :: println);
|
||||
}
|
||||
|
||||
private static void generateNumberToCharMap()
|
||||
{
|
||||
numberToCharMap = new Character[10][5];
|
||||
numberToCharMap[0] = new Character[]{'\0'};
|
||||
numberToCharMap[1] = new Character[]{'\0'};
|
||||
numberToCharMap[2] = new Character[]{'a','b','c'};
|
||||
numberToCharMap[3] = new Character[]{'d','e','f'};
|
||||
numberToCharMap[4] = new Character[]{'g','h','i'};
|
||||
numberToCharMap[5] = new Character[]{'j','k','l'};
|
||||
numberToCharMap[6] = new Character[]{'m','n','o'};
|
||||
numberToCharMap[7] = new Character[]{'p','q','r','s'};
|
||||
numberToCharMap[8] = new Character[]{'t','u','v'};
|
||||
numberToCharMap[9] = new Character[]{'w','x','y','z'};
|
||||
}
|
||||
|
||||
// Driver code
|
||||
public static void main(String[] args)
|
||||
{
|
||||
int number[] = {2, 3, 4};
|
||||
printWords(number);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user