fork download
  1. # your code goes here
  2. def longestKinterspaceSubstring(word, k):
  3. if not word:
  4. return ""
  5.  
  6. n = len(word)
  7. # dp[i] stores the length of the longest valid substring ending at index i
  8. dp = [1] * n
  9.  
  10. max_len = 1
  11. max_start = 0
  12.  
  13. for i in range(1, n):
  14. if abs(ord(word[i]) - ord(word[i - 1])) <= k:
  15. dp[i] = dp[i - 1] + 1
  16. else:
  17. dp[i] = 1
  18.  
  19. if dp[i] > max_len:
  20. max_len = dp[i]
  21. max_start = i - max_len + 1
  22.  
  23. return word[max_start : max_start + max_len]
Success #stdin #stdout 0.07s 13992KB
stdin
Standard input is empty
stdout
Standard output is empty