mirror of
				https://gitee.com/coder-xiaomo/leetcode-problemset
				synced 2025-11-04 19:53:12 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			33 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
			
		
		
	
	
			33 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
<p>Given a string <code>s</code> and a character <code>c</code> that occurs in <code>s</code>, return <em>an array of integers </em><code>answer</code><em> where </em><code>answer.length == s.length</code><em> and </em><code>answer[i]</code><em> is the <strong>distance</strong> from index </em><code>i</code><em> to the <strong>closest</strong> occurrence of character </em><code>c</code><em> in </em><code>s</code>.</p>
 | 
						|
 | 
						|
<p>The <strong>distance</strong> between two indices <code>i</code> and <code>j</code> is <code>abs(i - j)</code>, where <code>abs</code> is the absolute value function.</p>
 | 
						|
 | 
						|
<p> </p>
 | 
						|
<p><strong>Example 1:</strong></p>
 | 
						|
 | 
						|
<pre>
 | 
						|
<strong>Input:</strong> s = "loveleetcode", c = "e"
 | 
						|
<strong>Output:</strong> [3,2,1,0,1,0,0,1,2,2,1,0]
 | 
						|
<strong>Explanation:</strong> The character 'e' appears at indices 3, 5, 6, and 11 (0-indexed).
 | 
						|
The closest occurrence of 'e' for index 0 is at index 3, so the distance is abs(0 - 3) = 3.
 | 
						|
The closest occurrence of 'e' for index 1 is at index 3, so the distance is abs(1 - 3) = 2.
 | 
						|
For index 4, there is a tie between the 'e' at index 3 and the 'e' at index 5, but the distance is still the same: abs(4 - 3) == abs(4 - 5) = 1.
 | 
						|
The closest occurrence of 'e' for index 8 is at index 6, so the distance is abs(8 - 6) = 2.
 | 
						|
</pre>
 | 
						|
 | 
						|
<p><strong>Example 2:</strong></p>
 | 
						|
 | 
						|
<pre>
 | 
						|
<strong>Input:</strong> s = "aaab", c = "b"
 | 
						|
<strong>Output:</strong> [3,2,1,0]
 | 
						|
</pre>
 | 
						|
 | 
						|
<p> </p>
 | 
						|
<p><strong>Constraints:</strong></p>
 | 
						|
 | 
						|
<ul>
 | 
						|
	<li><code>1 <= s.length <= 10<sup>4</sup></code></li>
 | 
						|
	<li><code>s[i]</code> and <code>c</code> are lowercase English letters.</li>
 | 
						|
	<li>It is guaranteed that <code>c</code> occurs at least once in <code>s</code>.</li>
 | 
						|
</ul>
 |