Skip to content

Commit

Permalink
add 1408
Browse files Browse the repository at this point in the history
  • Loading branch information
luliyucoordinate committed Apr 18, 2020
1 parent 92c67d5 commit cdaf975
Show file tree
Hide file tree
Showing 7 changed files with 195 additions and 5 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -703,4 +703,5 @@ LeetCode
|1403|[Minimum Subsequence in Non-Increasing Order](https://leetcode.com/problems/minimum-subsequence-in-non-increasing-order/)|c|[c++](./src/1403-Minimum-Subsequence-in-Non-Increasing-Order/1403.cpp)|[python](./src/1403-Minimum-Subsequence-in-Non-Increasing-Order/1403.py)|[go](./src/1403-Minimum-Subsequence-in-Non-Increasing-Order/1403.go)|[js](./src/1403-Minimum-Subsequence-in-Non-Increasing-Order/1403.js)|[java](./src/1403-Minimum-Subsequence-in-Non-Increasing-Order/1403.java)|Easy|
|1404|[Number of Steps to Reduce a Number in Binary Representation to One](https://leetcode.com/problems/number-of-steps-to-reduce-a-number-in-binary-representation-to-one/)|c|[c++](./src/1404-Number-of-Steps-to-Reduce-a-Number-in-Binary-Representation-to-One/1404.cpp)|[python](./src/1404-Number-of-Steps-to-Reduce-a-Number-in-Binary-Representation-to-One/1404.py)|[go](./src/1404-Number-of-Steps-to-Reduce-a-Number-in-Binary-Representation-to-One/1404.go)|[js](./src/1404-Number-of-Steps-to-Reduce-a-Number-in-Binary-Representation-to-One/1404.js)|[java](./src/1404-Number-of-Steps-to-Reduce-a-Number-in-Binary-Representation-to-One/1404.java)|Medium|
|1405|[Longest Happy String](https://leetcode.com/problems/longest-happy-string/)|c|[c++](./src/1405-Longest-Happy-String/1405.cpp)|[python](./src/1405-Longest-Happy-String/1405.py)|[go](./src/1405-Longest-Happy-String/1405.go)|[js](./src/1405-Longest-Happy-String/1405.js)|[java](./src/1405-Longest-Happy-String/1405.java)|Medium|
|1406|[Stone Game III](https://leetcode.com/problems/stone-game-iii/)|c|[c++](./src/1406-Stone-Game-III/1406.cpp)|[python](./src/1406-Stone-Game-III/1406.py)|[go](./src/1406-Stone-Game-III/1406.go)|[js](./src/1406-Stone-Game-III/1406.js)|[java](./src/1406-Stone-Game-III/1406.java)|Hard|
|1406|[Stone Game III](https://leetcode.com/problems/stone-game-iii/)|c|[c++](./src/1406-Stone-Game-III/1406.cpp)|[python](./src/1406-Stone-Game-III/1406.py)|[go](./src/1406-Stone-Game-III/1406.go)|[js](./src/1406-Stone-Game-III/1406.js)|[java](./src/1406-Stone-Game-III/1406.java)|Hard|
|1408|[String Matching in an Array](String Matching in an Array)|c|[c++](./src/1408-String-Matching-in-an-Array/1408.cpp)|[python](./src/1408-String-Matching-in-an-Array/1408.py)|[go](./src/1408-String-Matching-in-an-Array/1408.go)|[js](./src/1408-String-Matching-in-an-Array/1408.js)|[java](./src/1408-String-Matching-in-an-Array/1408.java)|Easy|
43 changes: 43 additions & 0 deletions src/1408-String-Matching-in-an-Array/1408.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const int M = 50010;
int son[M][27], nodes[M], idx;

void insert(string str) {
int p = 0;
for (char c : str) {
int& s = son[p][c - 'a'];
if (!s) s = ++idx;
son[s][26]++;
p = s;
}
}

bool get(string& str) {
int p = 0, s;
for (char c : str) {
s = son[p][c - 'a'];
if (!s) return false;
p = s;
}
return son[s][26] > 1;
}

class Solution {
public:
vector<string> stringMatching(vector<string>& words) {
memset(son, 0, sizeof son);
memset(nodes, 0, sizeof nodes);
idx = 0;

for (string& word : words) {
for (int i = 0; i < word.size(); i++) {
insert(word.substr(i));
}
}

vector<string> res;
for (string& word : words) {
if (get(word)) res.emplace_back(word);
}
return res;
}
};
46 changes: 46 additions & 0 deletions src/1408-String-Matching-in-an-Array/1408.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
func stringMatching(words []string) []string {
root := &Trie{}

for _, word := range words {
for i := 0; i < len(word); i++ {
root.Insert(word[i:len(word)])
}
}

res := []string{}
for _, word := range words {
if root.Get(word) {
res = append(res, word)
}
}
return res
}

type Trie struct {
val int
next [26]*Trie
}

func (this *Trie) Insert(word string) {
cur := this
for i := 0; i < len(word); i++ {
j := word[i] - 'a'
if cur.next[j] == nil {
cur.next[j] = &Trie{}
}
cur = cur.next[j]
cur.val++
}
}

func (this *Trie) Get(word string) bool {
cur := this
for i := 0; i < len(word); i++ {
j := word[i] - 'a'
if cur.next[j] == nil {
return false
}
cur = cur.next[j]
}
return cur.val > 1
}
41 changes: 41 additions & 0 deletions src/1408-String-Matching-in-an-Array/1408.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
class Solution {
public List<String> stringMatching(String[] words) {
Trie root = new Trie();
for (String word : words) {
for (int i = 0; i < word.length(); i++) {
root.insert(word.substring(i));
}
}

List<String> res = new ArrayList();
for (String word : words) {
if (root.get(word)) res.add(word);
}
return res;
}
}

class Trie {
public int val;
public Trie[] next = new Trie[26];

public void insert(String str) {
Trie cur = this;
for (int i = 0; i < str.length(); i++) {
int j = str.charAt(i) - 'a';
if (cur.next[j] == null) cur.next[j] = new Trie();
cur = cur.next[j];
cur.val++;
}
}

public boolean get(String str) {
Trie cur = this;
for (int i = 0; i < str.length(); i++) {
int j = str.charAt(i) - 'a';
if (cur.next[j] == null) return false;
cur = cur.next[j];
}
return cur.val > 1;
}
}
39 changes: 39 additions & 0 deletions src/1408-String-Matching-in-an-Array/1408.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
var stringMatching = function(words) {
let root = new Trie();
for (let word of words) {
for (let i = 0; i < word.length; i++) {
root.insert(word.substring(i));
}
}

let res = [];
for (let word of words) {
if (root.get(word)) res.push(word);
}
return res;
};

var Trie = function() {
this.val = 0;
this.next = {};
};

Trie.prototype.insert = function(word) {
let cur = this;
for (let c of word) {
if (!cur.next[c]) {
cur.next[c] = new Trie();
}
cur = cur.next[c];
cur.val++;
}
};

Trie.prototype.get = function(word) {
let cur = this;
for (let c of word) {
if (!cur.next[c]) return false;
cur = cur.next[c];
}
return cur.val > 1;
}
20 changes: 20 additions & 0 deletions src/1408-String-Matching-in-an-Array/1408.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution:
def stringMatching(self, words: List[str]) -> List[str]:
def add(word):
node = root
for c in word:
node = node[c]
node['#'] = node.get('#', 0) + 1

def get(word):
node = root
for c in word:
if (node := node.get(c)) is None: return False
return node['#'] > 1

Trie = lambda: collections.defaultdict(Trie)
root = Trie()
for word in words:
for i in range(len(word)):
add(word[i:])
return [word for word in words if get(word)]
8 changes: 4 additions & 4 deletions src/addProb.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
import os, bisect

# 题目名称
name = "Stone Game III"
ID = 1406
url = "https://leetcode.com/problems/stone-game-iii/"
difficult = "Hard"
name = "String Matching in an Array"
ID = 1408
url = "https://leetcode.com/problems/string-matching-in-an-array/"
difficult = "Easy"
prog = ['c', 'cpp', 'py', 'go', 'js', 'java']


Expand Down

0 comments on commit cdaf975

Please sign in to comment.