My LeetCode grinding. Trying to do a problem a day.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

12345678910111213141516171819202122232425
  1. class Solution:
  2. def minSteps(self, s, t):
  3. # as it's only lowercase letters, we can simply count both then compare
  4. s_letters = [0] * 26
  5. t_letters = [0] * 26
  6. for a in s:
  7. s_letters[ord(a) - 97] += 1
  8. for a in t:
  9. t_letters[ord(a) - 97] += 1
  10. # we could go either way and the answer should be the same but let's turn t into s:
  11. # we could count the deletions needed and the additions needed, and they should cancel
  12. # out because they are equal length
  13. # so easier to count absolute difference and then halve it.
  14. edit = 0
  15. for x in range(0, 26):
  16. edit += abs(s_letters[x] - t_letters[x])
  17. return edit // 2
  18. s = Solution()
  19. print("Expected: 1")
  20. print("Got:", s.minSteps("aba", "bba"))