Skip to content

Commit d6e8afb

Browse files
author
Partho Biswas
committed
Longest_String_Chain
1 parent ac5fba7 commit d6e8afb

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -791,7 +791,7 @@ BFS, DFS, Dijkstra, Floyd–Warshall, Bellman-Ford, Kruskal, Prim's, Minimum Spa
791791

792792
### 4. Dynamic Programming
793793

794-
<details><summary>Problem sstatement from AlgoExpert.io with solutions and tutorials/videos</summary>
794+
<details><summary>Problem statement from AlgoExpert.io with solutions and tutorials/videos</summary>
795795
<p>
796796

797797
| # | Title | Solution | Tutorial | Level | Remarks |
@@ -810,14 +810,15 @@ BFS, DFS, Dijkstra, Floyd–Warshall, Bellman-Ford, Kruskal, Prim's, Minimum Spa
810810
|12| [Max_Profit_With_K_Transactions](algoexpert.io/questions/Max_Profit_With_K_Transactions.md) | [Python](algoexpert.io/python/Max_Profit_With_K_Transactions.py) |
811811
|13| [Palindrome_Partitioning_Min_Cuts](algoexpert.io/questions/Palindrome_Partitioning_Min_Cuts.md) | [Python](algoexpert.io/python/Palindrome_Partitioning_Min_Cuts.py) |
812812
|14| [Longest_Increasing_Subsequence](algoexpert.io/questions/Longest_Increasing_Subsequence.md) | [Python](algoexpert.io/python/Longest_Increasing_Subsequence.py) | [Video 1](https://www.youtube.com/watch?v=fV-TF4OvZpk), [Video 2](https://www.youtube.com/watch?v=CE2b_-XfVDk), , [Video 3](https://www.hackerrank.com/challenges/longest-increasing-subsequent/problem)|
813+
|15| [Longest_String_Chain](algoexpert.io/questions/Longest_String_Chain.md) | [Python](algoexpert.io/python/Longest_String_Chain.py) | --- |
813814

814815
</p>
815816
</details>
816817

817818

818819
### 5. Famous Algorithm
819820

820-
<details><summary>Problem sstatement from AlgoExpert.io with solutions and tutorials/videos</summary>
821+
<details><summary>Problem statement from AlgoExpert.io with solutions and tutorials/videos</summary>
821822
<p>
822823

823824
| # | Title | Solution | Tutorial | Level | Remarks |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
def longestStringChain(strings):
2+
stringChains = {}
3+
for string in strings:
4+
stringChains[string] = {"nextString": "", "maxChainLen": 1}
5+
6+
sortedString = sorted(strings, key=len)
7+
for string in sortedString:
8+
findLongestStringChain(string, stringChains)
9+
10+
return buildLongestStringChain(strings, stringChains)
11+
12+
13+
def findLongestStringChain(currentString, stringChains):
14+
for i in range(len(currentString)):
15+
smallerString = currentString[:i] + currentString[i + 1:]
16+
if smallerString in stringChains:
17+
smallerStringChainLen = stringChains[smallerString]["maxChainLen"]
18+
currentStringChainLen = stringChains[currentString]["maxChainLen"]
19+
if smallerStringChainLen + 1 > currentStringChainLen:
20+
stringChains[currentString]["maxChainLen"] = smallerStringChainLen + 1
21+
stringChains[currentString]["nextString"] = smallerString
22+
23+
24+
def buildLongestStringChain(strings, stringChains):
25+
maxChainLen = 0
26+
chainStartingString = ""
27+
for string in strings:
28+
if stringChains[string]["maxChainLen"] > maxChainLen:
29+
maxChainLen = stringChains[string]["maxChainLen"]
30+
chainStartingString = string
31+
32+
longestStringChain = []
33+
currentString = chainStartingString
34+
while currentString != "":
35+
longestStringChain.append(currentString)
36+
currentString = stringChains[currentString]["nextString"]
37+
return [] if len(longestStringChain) == 1 else longestStringChain
38+
39+
40+
41+
42+
43+

0 commit comments

Comments
 (0)