-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPalindrome Linked List.cpp
89 lines (74 loc) · 2.08 KB
/
Palindrome Linked List.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
class Node{
public:
bool isLeaf;
int value;
Node* next[26];
Node(bool b=false){
isLeaf = b;
value = -1;
fill_n(&next[0], 26, nullptr);
}
};
void insert(Node* root, string& word, int i){
Node* curr = root;
int idx;
for(char c: word){
idx = c-'a';
if(curr->next[idx]==nullptr)
curr->next[idx] = new Node();
curr = curr->next[idx];
}
curr->value = i;
curr->isLeaf = true;
}
void buildTries(Node* root, vector<string>& words){
for(int i=0;i<words.size();i++)
insert(root, words[i], i);
}
vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
vector<string> answer;
if(words.size()==0 || board.size()==0 || board[0].size()==0) return answer;
//unique(words.begin(), words.end());
Node* root = new Node();
buildTries(root, words);
Node* curr = root;
for(int i=0;i<board.size();i++){
for(int j=0;j<board[0].size();j++){
dfs(answer,board,words,curr,i,j);
}
}
return answer;
}
void dfs(vector<string>& answer, vector<vector<char> >& board, vector<string>& words ,Node* curr, int i, int j){
if(!inRange(board,i,j) || board[i][j]=='#') return;
if(curr->next[board[i][j]-'a']==nullptr) return;
if(curr->next[board[i][j]-'a']->isLeaf){
answer.push_back(words[curr->next[board[i][j]-'a']->value]);
curr->next[board[i][j]-'a']->isLeaf = false;
}
char temp = board[i][j];
board[i][j] = '#';
dfs(answer, board, words, curr->next[temp-'a'],i-1,j);
dfs(answer, board, words, curr->next[temp-'a'],i+1,j);
dfs(answer, board, words, curr->next[temp-'a'],i,j-1);
dfs(answer, board, words, curr->next[temp-'a'],i,j+1);
board[i][j] = temp;
}
bool inRange(vector<vector<char> >& board, int i, int j){
return i>=0 && i<board.size() && j>=0 && j<board[0].size();
}
};
int main(){
Solution s;
vector<vector<char> > v;
vector<char> vv = {'a', 'a'};
v.push_back(vv);
vector<string> words = {"a"};
s.findWords(v, words);
}