Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
183 changes: 183 additions & 0 deletions Others/Implementing_auto_completing_features_using_trie.java
Original file line number Diff line number Diff line change
@@ -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 changes: 159 additions & 0 deletions Others/Sudoku.java
Original file line number Diff line number Diff line change
@@ -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");
}
}
}
Loading