Given a string s, return the longest palindromic substring in s.
"aba" is also a valid answer.
1 <= s.length <= 1000s consist of only digits and English letters.How can we reuse a previously computed palindrome to compute a larger palindrome?
If “aba” is a palindrome, is “xabax” a palindrome? Similarly is “xabay” a palindrome?
Complexity based hint:
If we use brute-force and check whether for every start and end position a substring is a palindrome we have O(n2) start - end pairs and O(n) palindromic checks. Can we reduce the time for palindromic checks to O(1) by reusing some previous computation.
No developer has claimed the Ruby throne for LeetCode #5 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
# @param {String} s
# @return {String}
def longest_palindrome(s)
longest = s[0]
s = s.chars.join("#")
s.size.times do |i|
j = 1
while j <= i and i + j < s.size
break if s[i - j] != s[i + j]
if s[i - j] == '#'
j += 1
next
end
length = j * 2 + 1
if length > longest.size
longest = s[i - j..i + j]
end
j += 1
end
end
longest.gsub('#', '')
end
No developer has claimed the Python throne for LeetCode #5 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the Java throne for LeetCode #5 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the C++ throne for LeetCode #5 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the Javascript throne for LeetCode #5 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the Go throne for LeetCode #5 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the Rust throne for LeetCode #5 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the C# throne for LeetCode #5 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the Swift throne for LeetCode #5 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the Kotlin throne for LeetCode #5 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.