Skip to content

Commit 7750f08

Browse files
Merge pull request youngyangyang04#2668 from Yang-Changhui/master
add problems/kamacoder/0110.字符串接龙.md python
2 parents 66fa3d1 + 2272d0e commit 7750f08

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

problems/kamacoder/0110.字符串接龙.md

+32
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,38 @@ public class Main {
217217
```
218218

219219
### Python
220+
```Python
221+
def judge(s1,s2):
222+
count=0
223+
for i in range(len(s1)):
224+
if s1[i]!=s2[i]:
225+
count+=1
226+
return count==1
227+
228+
if __name__=='__main__':
229+
n=int(input())
230+
beginstr,endstr=map(str,input().split())
231+
if beginstr==endstr:
232+
print(0)
233+
exit()
234+
strlist=[]
235+
for i in range(n):
236+
strlist.append(input())
237+
238+
# use bfs
239+
visit=[False for i in range(n)]
240+
queue=[[beginstr,1]]
241+
while queue:
242+
str,step=queue.pop(0)
243+
if judge(str,endstr):
244+
print(step+1)
245+
exit()
246+
for i in range(n):
247+
if visit[i]==False and judge(strlist[i],str):
248+
visit[i]=True
249+
queue.append([strlist[i],step+1])
250+
print(0)
251+
```
220252

221253
### Go
222254

0 commit comments

Comments
 (0)