Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 394e0d7

Browse files
feat: add nim-lang solution to problem: No.0005 (doocs#597)
No.0005. Longest Palindromic Substring
1 parent 9b96711 commit 394e0d7

File tree

3 files changed

+76
-20
lines changed

3 files changed

+76
-20
lines changed

‎solution/0000-0099/0005.Longest Palindromic Substring/README.md‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,32 @@ public class Solution{
195195
}
196196
```
197197

198+
### **Nim**
199+
200+
```nim
201+
import std/sequtils
202+
203+
proc longestPalindrome(s: string): string =
204+
let n: int = s.len()
205+
var
206+
dp = newSeqWith[bool](n, newSeqWith[bool](n, false))
207+
start: int = 0
208+
mx: int = 1
209+
210+
for j in 0 ..< n:
211+
for i in 0 .. j:
212+
if j - i < 2:
213+
dp[i][j] = s[i] == s[j]
214+
else:
215+
dp[i][j] = dp[i + 1][j - 1] and s[i] == s[j]
216+
217+
if dp[i][j] and mx < j - i + 1:
218+
start = i
219+
mx = j - i + 1
220+
221+
result = s[start ..< start+mx]
222+
```
223+
198224
### **...**
199225

200226
```

‎solution/0000-0099/0005.Longest Palindromic Substring/README_EN.md‎

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,10 @@
66

77
<p>Given a string <code>s</code>, return&nbsp;<em>the longest palindromic substring</em> in <code>s</code>.</p>
88

9-
10-
119
<p>&nbsp;</p>
1210

1311
<p><strong>Example 1:</strong></p>
1412

15-
16-
1713
<pre>
1814

1915
<strong>Input:</strong> s = &quot;babad&quot;
@@ -24,12 +20,8 @@
2420

2521
</pre>
2622

27-
28-
2923
<p><strong>Example 2:</strong></p>
3024

31-
32-
3325
<pre>
3426

3527
<strong>Input:</strong> s = &quot;cbbd&quot;
@@ -38,12 +30,8 @@
3830

3931
</pre>
4032

41-
42-
4333
<p><strong>Example 3:</strong></p>
4434

45-
46-
4735
<pre>
4836

4937
<strong>Input:</strong> s = &quot;a&quot;
@@ -52,12 +40,8 @@
5240

5341
</pre>
5442

55-
56-
5743
<p><strong>Example 4:</strong></p>
5844

59-
60-
6145
<pre>
6246

6347
<strong>Input:</strong> s = &quot;ac&quot;
@@ -66,14 +50,10 @@
6650

6751
</pre>
6852

69-
70-
7153
<p>&nbsp;</p>
7254

7355
<p><strong>Constraints:</strong></p>
7456

75-
76-
7757
<ul>
7858
<li><code>1 &lt;= s.length &lt;= 1000</code></li>
7959
<li><code>s</code> consist of only digits and English letters (lower-case and/or upper-case),</li>
@@ -215,6 +195,32 @@ public class Solution{
215195
}
216196
```
217197

198+
### **Nim**
199+
200+
```nim
201+
import std/sequtils
202+
203+
proc longestPalindrome(s: string): string =
204+
let n: int = s.len()
205+
var
206+
dp = newSeqWith[bool](n, newSeqWith[bool](n, false))
207+
start: int = 0
208+
mx: int = 1
209+
210+
for j in 0 ..< n:
211+
for i in 0 .. j:
212+
if j - i < 2:
213+
dp[i][j] = s[i] == s[j]
214+
else:
215+
dp[i][j] = dp[i + 1][j - 1] and s[i] == s[j]
216+
217+
if dp[i][j] and mx < j - i + 1:
218+
start = i
219+
mx = j - i + 1
220+
221+
result = s[start ..< start+mx]
222+
```
223+
218224
### **...**
219225

220226
```
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import std/sequtils
2+
3+
proc longestPalindrome(s: string): string =
4+
let n: int = s.len()
5+
var
6+
dp = newSeqWith[bool](n, newSeqWith[bool](n, false))
7+
start: int = 0
8+
mx: int = 1
9+
10+
for j in 0 ..< n:
11+
for i in 0 .. j:
12+
if j - i < 2:
13+
dp[i][j] = s[i] == s[j]
14+
else:
15+
dp[i][j] = dp[i + 1][j - 1] and s[i] == s[j]
16+
17+
if dp[i][j] and mx < j - i + 1:
18+
start = i
19+
mx = j - i + 1
20+
21+
result = s[start ..< start+mx]
22+
23+
# Driver Code
24+
# echo(longestPalindrome("adbdaba"))

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /