Skip to content
This repository was archived by the owner on Jun 2, 2024. It is now read-only.

Commit f9dd6c3

Browse files
Word Search 2,Backtracking+Tries Question (#921)
* bt added * mergeSort
1 parent 61a5765 commit f9dd6c3

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed

C++/Backtracking/bt.cpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
class TrieNode{
4+
public:
5+
bool is_end;
6+
vector<TrieNode*> children;
7+
TrieNode(){
8+
is_end=false;
9+
children=vector<TrieNode*>(26, NULL);
10+
}
11+
};
12+
13+
class Trie{
14+
public:
15+
TrieNode* getRoot(){return root;}
16+
Trie(vector<string>& words){
17+
root=new TrieNode();
18+
for(int i=0; i<words.size(); ++i)
19+
addWord(words[i]);
20+
}
21+
void addWord(const string& word){
22+
TrieNode* cur=root;
23+
for(int i=0; i<word.size(); ++i){
24+
int index=word[i]-'a';
25+
if(cur->children[index]==NULL)
26+
cur->children[index]=new TrieNode();
27+
cur=cur->children[index];
28+
}
29+
cur->is_end=true;
30+
}
31+
private:
32+
TrieNode* root;
33+
};
34+
35+
class Solution {
36+
public:
37+
vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
38+
Trie* trie = new Trie(words);
39+
TrieNode* root=trie->getRoot();
40+
set<string> result_set;
41+
for(int x=0; x<board.size(); ++x)
42+
for(int y=0; y<board[0].size(); ++y)
43+
findWords(board, x, y, root, "", result_set);
44+
45+
vector<string> result;
46+
for(auto it:result_set) result.push_back(it);
47+
return result;
48+
}
49+
private:
50+
void findWords(vector<vector<char>>& board, int x, int y, TrieNode* root, string word, set<string>& result){
51+
if(x<0||x>=board.size()||y<0||y>=board[0].size() || board[x][y]==' ') return;
52+
53+
if(root->children[board[x][y]-'a'] != NULL){
54+
word=word+board[x][y];
55+
root=root->children[board[x][y]-'a'];
56+
if(root->is_end) result.insert(word);
57+
char c=board[x][y];
58+
board[x][y]=' ';
59+
findWords(board, x+1, y, root, word, result);
60+
findWords(board, x-1, y, root, word, result);
61+
findWords(board, x, y+1, root, word, result);
62+
findWords(board, x, y-1, root, word, result);
63+
board[x][y]=c;
64+
}
65+
}
66+
};
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import java.util.Scanner;
2+
public class Mergesort_recursive
3+
{
4+
public static void main(String a[])
5+
{
6+
int temp;
7+
Scanner sc = new Scanner(System.in);
8+
System.out.println("Enter array size");
9+
int n = sc.nextInt();
10+
int[] list = new int[n];
11+
System.out.println("Enter numbers ");
12+
for (int i = 0; i < n; i++)
13+
{
14+
int number = sc.nextInt();
15+
list[i] = number;
16+
}
17+
System.out.println("List before sorting \n");
18+
for (int i = 0; i < list.length; i++)
19+
System.out.print(list[i] + " ");
20+
System.out.println();
21+
22+
mergeSort(list, 0, list.length - 1);
23+
System.out.print("List after sorting \n");
24+
for (int i = 0; i < list.length; i++)
25+
System.out.print(list[i] + " ");
26+
System.out.println();
27+
}
28+
29+
public static void mergeSort(int list[], int low, int high)
30+
{
31+
if (low >= high)
32+
{
33+
return;
34+
}
35+
int middle = (low + high) / 2;
36+
mergeSort(list, low, middle);
37+
mergeSort(list, middle + 1, high);
38+
merge(list, low, middle, high);
39+
}
40+
private static void merge(int list[], int low, int middle, int high)
41+
{
42+
int IstList_end = middle;
43+
int IIndList_start = middle + 1;
44+
int l = low;
45+
while ((l <= IstList_end) && (IIndList_start <= high))
46+
{
47+
if (list[low] < list[IIndList_start])
48+
{
49+
50+
low++;
51+
}
52+
else
53+
{
54+
int temp = list[IIndList_start];
55+
56+
for (int j = IIndList_start - 1; j >= low; j--)
57+
{
58+
59+
list[j + 1] = list[j];
60+
}
61+
list[low] = temp;
62+
low++;
63+
IstList_end++;
64+
IIndList_start++;
65+
}
66+
}
67+
}
68+
}

0 commit comments

Comments
 (0)