일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 715. Range Module
- data science
- Regular Expression
- Decorator
- Generator
- t1
- Protocol
- 컴퓨터의 구조
- 30. Substring with Concatenation of All Words
- 43. Multiply Strings
- LeetCode
- DWG
- Class
- attribute
- 운영체제
- concurrency
- shiba
- Python
- 시바견
- iterator
- Python Implementation
- 밴픽
- kaggle
- 315. Count of Smaller Numbers After Self
- Substring with Concatenation of All Words
- 109. Convert Sorted List to Binary Search Tree
- Convert Sorted List to Binary Search Tree
- 파이썬
- Python Code
- 프로그래머스
Archives
- Today
- Total
Scribbling
[LeetCode] 2781. Length of the Longest Valid Substring 본문
Computer Science/Algorithms & Data Structures
[LeetCode] 2781. Length of the Longest Valid Substring
focalpoint 2024. 7. 10. 03:03https://leetcode.com/problems/length-of-the-longest-valid-substring/description/
The main idea is to use Trie data structure.
1. Python
class Solution:
def longestValidSubstring(self, word: str, forbidden: List[str]) -> int:
ret = 0
trie = {}
for f in forbidden:
f = f[::-1]
t = trie
for c in f:
if c not in t:
t[c] = {}
t = t[c]
t['#'] = len(f)
j = 0
for i in range(len(word)):
valid = True
nxtJ = None
t = trie
for k in range(i, j - 1, -1):
c = word[k]
if c not in t:
break
t = t[c]
if '#' in t:
valid = False
nxtJ = i - t['#'] + 2
break
if valid is True:
ret = max(ret, i - j + 1)
else:
j = nxtJ
return ret
2. C++
class TrieNode {
public:
char ch;
bool isWord;
int length;
TrieNode* children[26] {nullptr};
TrieNode() {}
TrieNode(char ch, bool isWord=false, int length=0): ch(ch), isWord(isWord), length(length) {}
~TrieNode() {}
};
class Trie {
public:
TrieNode* root = new TrieNode();
Trie () {}
void insert(string word) {
TrieNode* cur = root;
for (char c : word) {
int idx = c - 'a';
if (cur->children[idx] == nullptr) {
cur->children[idx] = new TrieNode(c);
}
cur = cur->children[idx];
}
cur->isWord = true;
cur->length = word.size();
}
~Trie() {
delete root;
}
};
class Solution {
public:
int longestValidSubstring(string word, vector<string>& forbidden) {
int ret = 0;
Trie trie = Trie();
for (string& word : forbidden) {
reverse(word.begin(), word.end());
trie.insert(word);
}
int j = 0;
for (int i=0; i<word.size(); i++) {
bool valid = true;
int nxtJ = -1;
TrieNode node = *trie.root;
for (int k=i; k>j-1; k--) {
char c = word[k];
int idx = c - 'a';
if (node.children[idx] == nullptr) {
break;
}
node = *node.children[idx];
if (node.isWord) {
valid = false;
nxtJ = i - node.length + 2;
break;
}
}
if (valid) {
ret = max(ret, i - j + 1);
} else {
j = nxtJ;
}
}
return ret;
}
};
'Computer Science > Algorithms & Data Structures' 카테고리의 다른 글
[LeetCode] 2340. Minimum Adjacent Swaps to Make a Valid Array (0) | 2024.07.02 |
---|---|
[Programmers] 방의 개수 (0) | 2024.07.02 |
[Programmers] 순위 (0) | 2024.06.28 |
[Programmers] 가장 먼 노드 (0) | 2024.06.28 |
[Programmers] 입국심사 (0) | 2024.06.27 |