Leetcode - Break a Palindrome (Python)

Sdílet
Vložit
  • čas přidán 6. 09. 2024
  • September 2021 Leetcode Challenge
    Leetcode - Break a Palindrome #1328
    Difficulty: Medium

Komentáře • 7

  • @icaryslittle6370
    @icaryslittle6370 Před 2 lety +5

    No need to go through it twice ... If the 'a' doesn't work and the string is more than one character, just make 'b' the last letter ...

    • @timc3406
      @timc3406  Před 2 lety +1

      Lol you are correct! Nice catch

    • @AM-wx9zl
      @AM-wx9zl Před rokem

      Doesn't work for "aba"

    • @icaryslittle6370
      @icaryslittle6370 Před rokem +1

      @@AM-wx9zl Yes, it does -- going through 'aba' once doesn't work, b/c 'aaa' is a palindrome, so if you make the last letter 'b', you get 'abb', which is the correct answer ...

    • @AM-wx9zl
      @AM-wx9zl Před rokem

      @@icaryslittle6370 you've already changed it to 'aaa' at that point, swapping the last letter to get 'aab' is the wrong answer. I may have misunderstood your original comment, do you have a code example? I couldn't get it to work but I'm not great at leetcode

    • @icaryslittle6370
      @icaryslittle6370 Před rokem

      Not that good *YET* ... Keep practicing, brother ...
      class Solution(object):
      def breakPalindrome(self, palindrome):
      if len(palindrome) == 1:
      return ''
      p = list(palindrome)
      for x in range(len(p)/2):
      temp = p[x]
      p[x] ='a'
      if p != p[::-1]:
      return ''.join(p)
      p[x] = temp
      p[-1] = 'b'
      if p != p[::-1]:
      return ''.join(p)
      return ''

  • @SeethaYt
    @SeethaYt Před 2 lety

    Op .... Means orey puka ✅✅💥💥