6 kyu

longest_palindrome

4,283 of 11,088kgashok
Description
Loading description...
Fundamentals
  • Please sign in or sign up to leave a comment.
  • sidwick Avatar

    Nice Kata!

  • sergeyklochko Avatar

    With s = "lpllmdokuhqokgkmwyrfuzzcjgozlkqiqmhgdmvopiqkdlppqxjwyiyvhbchfvvsr": 3 should equal 5

    Can anyone help me to find 5?

  • XoRMiAS Avatar

    Updated JavaScript

  • Bigoh20 Avatar

    Nice one

  • JohanWiltink Avatar

    ( JS )

    Tests do not allow updating to Node 14.x; they time out. It appears the kata requires micro-optimisation. ( It's also lacking random tests. )

    It would be very good to have this kata updated to Node 14.x, so Node 10.x can be removed from Codewars. This is one of no more than twenty-odd kata stuck in Node 10.x ( see https://github.com/codewars/content-issues/wiki/List-of-JavaScript-Kata-to-Update#list-of-node-10x-kata ).

  • snakesu Avatar

    The "attempt" tests are on the edge of timing out in javascript... It may be worth reducing their required time a little (e. g. reduce the number of tests...) It makes you think really hard about optimization, that's good. But even with optimization it does pass only sometimes (i.e. not timing out)...

  • ashishkag Avatar

    WHat is the input format ? Consists of only lowercase letters or any kind of characters? Do space separated palindromes or palindromes formed using mixed case letters count ? for ex : In string "I coulD am madl bb" , which is largest palindrome substring "lD am madl", "am ma" or "bb"

  • akar-0 Avatar

    Python fork updated to new framework + added random tests (O(n^2) solutions should still pass).

  • Ciniec Avatar

    For string "abcdefghba" the result should be 2 in my opinion:

    "ab" -> "ba"

    So why tests show me that expected value is 1?

    Java

  • agokh Avatar

    I believe this is a little bit hard for 6kyu; nevertheless, it was a great practice.

  • user4640159 Avatar

    This comment has been hidden.

  • YuliyaSi Avatar

    STDERR Unhandled rejection TestError: Expected: 1, instead got: 2

    Why??

  • nuccelli Avatar

    Using nested loops my code works fine in tests but when I try to attempt it returns Out of Time Execution :( , any hint? :p

  • user1430804 Avatar

    what an amazing kata this was.. thank you!

  • farhanaditya Avatar

    No random tests in JS

  • JDKoder Avatar

    I recommend adding a tests for repeating characters in a string of more than 20 characters.
    eg. doTest("lolollolollolollolol", 20); doTest("abcdefghijklmnopqrstuvwxyz lolollolollolollolol", 20);

    Reason being my solution was very different from most others and I could have submitted without fixing these failing cases.

  • MDSOPAN Avatar

    UndefinedBehaviorSanitizer:DEADLYSIGNAL ==1==ERROR: UndefinedBehaviorSanitizer: SEGV on unknown address 0x000000000000 (pc 0x000000425a43 bp 0x7ffca798fd30 sp 0x7ffca798f7b0 T1) ==1==The signal is caused by a READ memory access. ==1==Hint: address points to the zero page. ==1==WARNING: invalid path to external symbolizer! ==1==WARNING: Failed to use and restart external symbolizer! #0 0x425a42 (/workspace/test+0x425a42) #1 0x428802 (/workspace/test+0x428802) #2 0x426d2e (/workspace/test+0x426d2e) #3 0x42675d (/workspace/test+0x42675d) #4 0x42644b (/workspace/test+0x42644b) #5 0x42d5f5 (/workspace/test+0x42d5f5) #6 0x425c3d (/workspace/test+0x425c3d) #7 0x7fdf3a378bf6 (/lib/x86_64-linux-gnu/libc.so.6+0x21bf6) #8 0x404909 (/workspace/test+0x404909)

    UndefinedBehaviorSanitizer can not provide additional info. ==1==ABORTING

    i get this error and am not understanding why this happens. The code runs normally on Sublime.

  • marchukse Avatar

    This comment has been hidden.

  • Shunketsu Avatar

    I'd like to know what the optimal time complexity for this would be

  • OrdinaryMind Avatar

    Question about spaces: In example "I like racecars..." would "I li" count as a palindrome or not? Without space: "ili". Suggests making this clear by test or in description.

  • Lrac Avatar

    This comment has been hidden.

  • will252 Avatar

    Hi. Enjoying the problem, but not sure how this works, based on the following:

    Shoud this be 1?

    Test.assertNotEquals(longestPalindrome("zyabyz"), 6...

    And think this might be 5 - the "zzbaa" combination(?) From the examples: "zzbaabcd" -> 4

    Thought I understood the problem, but having seen these, I'm not sure of the rules. Thanks!

  • user9644768 Avatar

    Ruby 3.0 should be enabled, see this to learn how to do it

  • cubking Avatar

    The return value of the parameter abcdefghba should be 2.

  • liorEchoG Avatar

    I think it would be nice to state that white spaces are included for the palindrome count in the kata description.

  • MoAz101 Avatar

    Find the length of the longest substring in the given string s that is the same in reverse.

    As an example, if the input was “I like racecars that go fast”, the substring (racecar) length would be 7

    somebody please explain what he wants ME to do here?? like whats the goal what should i do am so confused

    every 6kyu question is like that,its just so hard to understand

  • Horhi Avatar

    There is an UB in C++ tests.

    UndefinedBehaviorSanitizer:DEADLYSIGNAL
    ==1==ERROR: UndefinedBehaviorSanitizer: SEGV on unknown address 0x000000000000 (pc 0x000000425aa6 bp 0x000000000001 sp 0x7fff2993d670 T1)
    ==1==The signal is caused by a READ memory access.
    ==1==Hint: address points to the zero page.
    ==1==WARNING: invalid path to external symbolizer!
    ==1==WARNING: Failed to use and restart external symbolizer!
        #0 0x425aa5  (/workspace/test+0x425aa5)
        #1 0x428abc  (/workspace/test+0x428abc)
        #2 0x42701e  (/workspace/test+0x42701e)
        #3 0x426a4d  (/workspace/test+0x426a4d)
        #4 0x42673b  (/workspace/test+0x42673b)
        #5 0x42d6b5  (/workspace/test+0x42d6b5)
        #6 0x425f2d  (/workspace/test+0x425f2d)
        #7 0x7f2da1f49bf6  (/lib/x86_64-linux-gnu/libc.so.6+0x21bf6)
        #8 0x4048c9  (/workspace/test+0x4048c9)
    
    UndefinedBehaviorSanitizer can not provide additional info.
    ==1==ABORTING
    
  • kylesq9 Avatar

    I'm getting this strange error in Python when I try to retreive the first item in the list. It says that list index 0 is out of range, meanwhile when I run my code through other python compilers like Python Tutor I am not receiving that error.

    Traceback (most recent call last): File "main.py", line 3, in test.assert_equals(longest_palindrome("a"), 1) File "/home/codewarrior/solution.py", line 23, in longest_palindrome longest = palinCounts[0] IndexError: list index out of range

  • GSAK-python Avatar

    What time should be a optimal for the last test (the long one)?

  • clairefro Avatar

    shouldn't test case "aab" -> 3 ? It says it returns two, but "aba" is 3 chars long

  • daedie Avatar

    In the Gettysburg address test case, I found 'ranynar', which is a palindrome of length 7. But it returns saying it should be zero. Is this an error, or am I making a mistake?

  • Calvin0125 Avatar

    This comment has been hidden.

  • Oleksiy__ Avatar

    is can "(nothing)" be palindrom? size of "(nothing)" == 0 or "(nothing)" == 1? nothing = "space"

  • sabadoryo Avatar

    This comment has been hidden.

  • ykIsCoding Avatar

    This comment has been hidden.

  • ykIsCoding Avatar

    This comment has been hidden.

  • mattcree Avatar

    You might want to add Test.assertEquals(longestPalindrome("abcacba"), 7) as a test case. I've seen a solution which effectively iterates the array, for each character looks up the next index of of that character and decides if it's a palindrome -- this case will fail that method because it will only consider bcacb as a palindrome, since a -> a would only get the first half of the palindrome in the string.

  • Beerus789 Avatar

    This comment has been hidden.

  • gmanley253 Avatar

    These are not palindromes! Palindromes are the same forward and backward. Racecar is a palindrome. Racecars is not a palindrome because it would be sracecar when spelled backward. No one refers to a palindrome within a word or string of characters. If you need help on what a palindrome is, reference the link.

    https://examples.yourdictionary.com/palindrome-examples.html

  • niminuko Avatar

    This comment has been hidden.

  • JoeWolfe12 Avatar

    Keeps on timing out - not sure if the server or me, but solution works.

  • Tonsias Avatar

    I like it, it's easy to understand but a bit challenging. Brute force is possible, making this kata solvable for many at this level.

  • 3one Avatar

    After dozens of timed out errors I gave up, tried an accepted solution from the Solutions section and it also timed out. More specifically sometimes it times out, sometimes it doesn't. This Kata is bad because it times out on an accepted solution 50% of the time. It should be removed.

  • 3one Avatar

    This comment has been hidden.

  • MohammedAbdeen Avatar

    why (longest_palindrome("abcdefghba"), 1) should equal 1 ? the result should equal 2 because substring "ab" it is right !

  • ledrago Avatar

    I am at my third working solution which make time out on the attemps solution. I'm a bit tired.

  • mrkotor2 Avatar

    This comment has been hidden.

  • Bit01 Avatar

    Does anybody know why the Java test expects 1 instead of 2 for the string "zyabyz"?

  • DolphieDude Avatar

    Can't deal with 1169 length. Time out old friend

  • ᛚᚨᚱᛊ ᚺᛖᚾᚱᛁᚲ Avatar

    Python: No randomized testing

  • JL2210 Avatar

    Missing language. Nice.

  • lawscott Avatar

    This comment has been hidden.

  • Matteo Zattera Avatar

    6 kyu? This kata is so difficult

  • Marz3587 Avatar

    ISSUE: my code is failing because "Expected: equal to 0, Actual: 1"

    That's the test's fault though right? you cant have a string and not have a letter and a letter is a palindrome of length 1.

  • xuanhoai Avatar

    doTest("zyabyz", 1); z to z is 6. why 1? can who explain, please

  • Hakeemmidan Avatar

    Ruby

    So my code works for the sample tests but not for the test cases. Here's what I get: " Time: 1801ms Passed: 6 Failed: 1 Exit Code: 1 Test Results: Test Passed: Value == 1 Test Passed: Value == 2 Test Passed: Value == 2 Test Passed: Value == 2 Test Passed: Value == 4 Test Passed: Value == 9 Expected: 1, instead got: 5 " Is anyone facing the same issue?

    Also, how can I print the all tests (or the failed tests) in the output? I read a couple responses saying that I should do that, but I don't know how to. Plz reply if you know how (for Ruby).

    Thanks

  • jsonhua1111 Avatar

    I don't understand this question.

  • antx_ Avatar

    I did not receive any out of bounds exception (I did my solution in C), instead the system compared the characters to something out of bounds which made me fail some tests. When I fetched out of bounds before they could happen, everything was fine. Dunno if that's the right place to put in this feedback, I'm relatively new here :-). Cheers!

  • coenaerp Avatar

    This comment has been hidden.

  • teddy shell Avatar

    This comment has been hidden.

  • clcraig Avatar

    New translations available: C, C++, and Java

  • RapidR3D Avatar

    This comment has been hidden.

  • le-du6 Avatar

    I don't understand why "abcdefghba" is a palindrome at all, and with "length 1" ?

  • philshaughnes Avatar

    This comment has been hidden.

  • zanemiel Avatar

    This comment has been hidden.

  • IBwWG Avatar

    Perhaps some more tests are needed to cover more complicated strings: http://stackoverflow.com/a/16776621/16777

  • kar_ua Avatar

    My solutions takes about ~450ms for all cases, but other users think that solution that takes ~2.5 seconds is the cleverest and that is the best practices. I think that is is wrong. Probably solutions should have some additional indicators ?

  • ben8p Avatar

    I see many implemetation with O(n*n) complexity. It would be great to add a huge test case (like 10.000 characters) so solutions which are too slow are excluded

  • ikmen Avatar

    This comment has been hidden.

  • ophunt Avatar

    Getting an error on tests and submit that doesn't seem to have anything to do with my code.

    STDERR: Error: unrecognized flag --no-warnings Try --help for options

    I'm passing all 10 tests on submit, but it fails because of this error.

  • aeftekhari Avatar

    This comment has been hidden.

  • iCyMind Avatar

    nodejs v6.0 support please~

  • alanwar Avatar

    This comment has been hidden.

  • szymonm Avatar

    Hello,

    I have a problem. All my tests are passing, keep failing on submit. ✘ Expected: 1, instead got: 2 without any explanation what is the failing test about. Please advise.

    Below is a dump from the console:

    Object {type: "execution success", stdout: "PASSED::Test Passed: Value == 1↵PASSED::Test P…Value == 9↵FAILED::Expected: 1, instead got: 2↵", stderr: " expect&#39;: Expected: 1, instead got: 2 (Test::…rom assert_equals'↵ from &lt;main&gt;&#39;↵", exitCode: 1, wallTime: 345…} exitCode : 1 languageVersion : "2.3.x" messageTime : 5 queueTime : 3 receivedAt : 1470168743719 responseAt : 1470168744253 result : Object setupTime : 181 stderr : " expect': Expected: 1, instead got: 2 (Test::Error)↵ from assert_equals&#39;↵ from <main>'↵" stdout : "PASSED::Test Passed: Value == 1↵PASSED::Test Passed: Value == 2↵PASSED::Test Passed: Value == 2↵PASSED::Test Passed: Value == 2↵PASSED::Test Passed: Value == 4↵PASSED::Test Passed: Value == 9↵FAILED::Expected: 1, instead got: 2↵" token : "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJjaXBoZXJlZCI6WyJzZXR1cCIsImZpeHR1cmUiXSwiaWQiOiI1N2EwNjYyNzUzYmEzMzc1OWEwMDAwOGYiLCJjb21wbGV0ZWQiOmZhbHNlLCJ3YWxsVGltZSI6MzQ1LCJpc3MiOiJjb2RlcnVubmVyIiwiZXhwIjoxNDcwMTY4ODY0MjUzfQ.q4v2BKYzYlvOguBRZAem6l-qrIvou6Y5OYG0UpqyCFk" totalTime : 534 type : "execution success" wallTime : 345

  • mjohnson2016 Avatar

    When I 'Run Tests', they all pass and when I Submit, I receive 1 failure. Problem is, the test is not shown so I don't know why it's failing.

    This is all I see: ✔ Test Passed: Value == 1
    ✔ Test Passed: Value == 2
    ✔ Test Passed: Value == 2
    ✔ Test Passed: Value == 2
    ✔ Test Passed: Value == 4
    ✔ Test Passed: Value == 9
    ✘ Expected: 1, instead got: 2

  • orenmizr Avatar

    all my tests are passing. keep failing on submit. getting expected 1 got 0. i covered '' and 'a' - what else?

  • 6eff Avatar

    can't submit final solution. receive server time outs. don't know is it a server faoult or my code. does anyone have final tests?

  • machinedarkly Avatar

    This comment has been hidden.

  • smashzen Avatar

    When I click "run tests" I get 7 of 7 passed. When I try to "submit", I get 7 pass and 1 fail. But there's no way to see this 8th test or find out why my code failed. Am I missing something?

  • crwhite3 Avatar

    This comment has been hidden.

  • MrPlazmaDude Avatar

    Test cases need to include titleized words as well. Or the description should be updated to indicate the exclusion of mismatching case palindromes (Racecar != racecaR).

    Some solutions will return 5 for "Racecar" rather than 7 but still pass.

    Unless my understanding of a palindrome is incorrect, racecar is still a palindrome even when it's titleized (Racecar).

  • megawatt Avatar

    In the example:

    "a" -> 1 
    "aab" -> 2  
    "abcde" -> 1
    "zzbaabcd" -> 4
    "" -> 0
    

    Shouldn't "zzbaabcd" return 5?

  • PaulCalvelage Avatar

    This comment has been hidden.

  • anter69 Avatar

    Needs random tests (and probably a bit more clear description)

  • tchar Avatar

    Test Case Problem: test.assert_equals(longest_palindrome("abcdefghba"), 1) Are you sure this equals 1? Because I can see 'ab' both in 'abcdefghba' and in the reversed one.

  • wohlfea Avatar

    I'm having a problem when submitting in python. I pass all of the test cases, no problem but then when I go to submit I'm getting this:

    'Process was terminated. It took longer than 6000ms to complete'

    Anyone else experiencing this issue? I am given no other information... Should I post my code here?

  • pmdesgn Avatar

    All my pre-test and submit tests pass, except the very last one: "Expected: 1, instead got: 2". Love the site but lack of hints takes a lot of fun out of it. How about allowing the user to reveal a test case (perhaps at the expense of points)?

  • kgashok Avatar

    I fixed that test case. Please check now.

  • ToshiCG Avatar

    In "Your Test Cases": Test.assertEquals(longestPalindrome("zyabyz", 0, "Are you sure that is a palindrome?") should be: Test.assertEquals(longestPalindrome("zyabyz"), 1, "Are you sure that is a palindrome?") There is a parenthesis missing and the expected value is wrong, should be 1 rather then 0;

    More tests including some random ones for the final submission might be good.

  • barrenec Avatar

    As an example, if the input was “I like racecars that go fast”, the substring length would be 7. i guess you mean racecar and not racecars

  • syllie Avatar

    This comment has been hidden.

  • user6361752 Avatar

    Add to description: if 0 return 0

  • anson1138 Avatar

    when you submit your final submission and the tests fail, there's no indication where it failed.

  • pinxue Avatar

    "abhgfedcba" should be 2, the test case is wrong as 1!

  • croceflamenco Avatar

    I enjoyed this one very much, so thank you.

    You may want to clarify that none of the test cases actually include whitespace characters (as in the example sentence given in the kata description), or consider adding some that do.

  • Hendrycks Avatar

    Indicate whether the algorithm can be O(n^3) or if it must be, at worst, O(n^2). The O(n^2) situation warrants this kata being at most 5 kyu.

  • zegomesjf Avatar

    this is my first exercise, thank you for your time and work.

    so... I had a problem here.

    All the tests passed when I run the tests, but the last one fail when I submit.

    Running tests Test Passed: Value == 1 Test Passed: Value == 2 Test Passed: Value == 2 Test Passed: Value == 2 Test Passed: Value == 4 Test Passed: Value == 9 6 Passed 0 Failed 0 Errors

    When I submit Test Passed: Value == 1 Test Passed: Value == 2 Test Passed: Value == 2 Test Passed: Value == 2 Test Passed: Value == 4 Test Passed: Value == 9 Expected: 1, instead got: 9 6 Passed 1 Failed 0 Errors Process took 74ms to complete

    What is the problem?

  • googamanga Avatar

    This comment has been hidden.

  • bkaes Avatar

    Typo in JavaScript: longestPalidrome should be longestPalindrome (missing n).

  • GiacomoSorbi Avatar

    JS and Ruby translations submitted, if you are interested :)

  • jolaf Avatar

    To me, this test seems wrong:

    test.assert_equals(longest_palindrome("abcdefghba"), 0)

    The right answer should be 1, consistent with

    test.assert_equals(longest_palindrome("a"), 1)

    The only case when the function should return 0, I think, is when the string is empty.

  • jolaf Avatar

    The tests should go the other way around.

    test.assert_equals(longest_palindrome("a"), 1)

    instead of

    test.assert_equals(1, longest_palindrome("a"))

    othewise when test fails it prints, for example

    1 should equal 0

    while it should print

    0 should equal 1