Leetcode0821. Shortest distance between characters (simple)

content

1. Problem description

2. Problem solving analysis

 3. Code implementation


1. Problem description

You are given a string  s and a character   that  c has   appeared in .cs

Returns an array of integers  answer where  answer.length == s.length and  answer[i] is   the  distances  from the subscript  i to its  nearest  character   .c

The distance  between the  two subscripts  i and   is   , where   is the absolute value function.jabs(i - j)abs

Example 1:

Input: s = "loveleetcode", c = "e"
 Output: [3,2,1,0,1,0,0,1,2,2,1,0]
 Explanation: The character 'e' appears at subscript 3 , 5, 6 and 11 (subscripts start at 0). 
The 'e' closest to index 0 occurs at index 3, so the distance is abs(0 - 3) = 3. 
The 'e' closest to index 1 occurs at index 3, so the distance is abs(1 - 3) = 2. 
For subscript 4, the 'e' that appears at both subscript 3 and subscript 5 is the closest to it, but the distance is the same abs(4 - 3) == abs(4 - 5) = 1 . 
The closest 'e' to subscript 8 occurs at subscript 6, so the distance is abs(8 - 6) = 2.

Example 2:

Input: s = "aaab", c = "b"
 Output: [3,2,1,0]

hint:

  • 1 <= s.length <= 104
  • s[i] and  c are lowercase English letters
  • The title data is guaranteed  to appear c at  s least once in

Source: LeetCode
Link: https://leetcode-cn.com/problems/shortest-distance-to-a-character The
copyright belongs to LeetCode.com. For commercial reprints, please contact the official authorization, and for non-commercial reprints, please indicate the source.

2. Problem solving analysis

         The point is that the character c may appear more than once.

        The character c separates the entire string into several intervals.

        Assuming that c appears n times in total, and the position of the kth occurrence of c is c_k, then:

  • c_1The distance from each subscript i to the nearest c from the beginning of s isc_1-i
  • The distance from c_neach subscript i to the end of s to the nearest c isi-c_n
  • The distance from each subscript i in the middle interval [c_l, c_{l+1}]to the nearest c ismin((i-c_l), (c_{l+1}-i))

 

 3. Code implementation

from typing import List

class Solution:
    def shortestToChar(self, s: str, c: str) -> List[int]:
        c_pos = []
        for k in range(len(s)):
            if s[k]==c:
                c_pos.append(k)
        # print(c_pos)
        ans = len(s)*[0]
        for i in range(c_pos[0]):
            ans[i] = c_pos[0] - i
        for i in range(c_pos[-1],len(s)):
            ans[i] = i - c_pos[-1]
        
        for k in range(0,len(c_pos)-1):
            for i in range(c_pos[k],c_pos[k+1]):
                # print(c_pos[k],c_pos[k+1])
                ans[i] = min(i-c_pos[k],c_pos[k+1]-i)
        
        return ans

if __name__ == "__main__":
    
    sln = Solution()
    
    s = "loveleetcode"
    c = "e"
    print(sln.shortestToChar(s, c))
    
    s = "aaab"
    c = "b"
    print(sln.shortestToChar(s, c))

        Execution time: 40 ms, beat 78.89% of users in all Python3 commits

        Memory consumption: 15 MB, beats 44.03% of users across all Python3 commits

 

        Back to the main directory: Leetcode daily problem-solving notes (dynamic update...)

 

Guess you like

Origin blog.csdn.net/chenxy_bwave/article/details/124263995