Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Uh oh!
There was an error while loading.
Please reload this page
.
algorithm-programming
/
AlgorithmsByPython
Public
forked from
Jack-Lee-Hiter/AlgorithmsByPython
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Files
Expand file tree
master
Breadcrumbs
AlgorithmsByPython
/
CheckErrorWord.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
44 lines (39 loc) · 1.46 KB
master
Breadcrumbs
AlgorithmsByPython
/
CheckErrorWord.py
Copy path
Top
File metadata and controls
Code
Blame
44 lines (39 loc) · 1.46 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
# 网传鹅厂面试题,英语单词拼写检查算法
# 比如输入hello, 却错误的输入了hellu, 找出出错的字母
# 感谢知乎知友@Lee Shellay
# 对词典中的每个词, 逐刺逐字母拓展Trie, 单词完结处结点用END符号标识
END
=
'$'
def
make_trie
(
words
):
trie
=
{}
for
word
in
words
:
t
=
trie
for
c
in
word
:
if
c
not
in
t
:
t
[
c
]
=
{}
t
=
t
[
c
]
t
[
END
]
=
{}
return
trie
# 容错查找
# 实质上是对Trie的深度优先搜索,每一步加深时就消耗目标词的一个字母
# 当搜索到达某个结点时,分为不消耗容错数和消耗容错数的的情形,继续搜索知道目标词为空。
# 搜索过程中,用path记录搜索路径,该路径及为一个词典中存在的词,作为纠错的参考
# 最终结果即为诸多搜索停止位置的结点路径的并集
def
check_fuzzy
(
trie
,
word
,
path
=
''
,
tol
=
1
):
#tol为容错数
if
word
==
''
:
return
[
path
]
if
END
in
trie
else
[]
else
:
p0
=
[]
if
word
[
0
]
in
trie
:
p0
=
check_fuzzy
(
trie
[
word
[
0
]],
word
[
1
:],
path
+
word
[
0
],
tol
)
p1
=
[]
if
tol
>
0
:
for
k
in
trie
:
if
k
!=
word
[
0
]:
p1
.
extend
(
check_fuzzy
(
trie
[
k
],
word
[
1
:],
path
+
k
,
tol
-
1
))
return
p0
+
p1
# 测试代码
words
=
[
'hello'
,
'hela'
,
'dome'
]
t
=
make_trie
(
words
)
print
(
t
)
print
(
check_fuzzy
(
t
,
'hellu'
))
print
(
check_fuzzy
(
t
,
'healu'
,
tol
=
2
))
You can’t perform that action at this time.