Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ca4bdeb

Browse filesBrowse files
authored
Create 07 closestChar.py
1 parent 01b1729 commit ca4bdeb
Copy full SHA for ca4bdeb

File tree

Expand file treeCollapse file tree

1 file changed

+14
-0
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+14
-0
lines changed
+14Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution(object):
2+
def shortestToChar(self, S, C):
3+
prev = float('-inf')
4+
ans = []
5+
for i, x in enumerate(S):
6+
if x == C: prev = i
7+
ans.append(i - prev)
8+
9+
prev = float('inf')
10+
for i in xrange(len(S) - 1, -1, -1):
11+
if S[i] == C: prev = i
12+
ans[i] = min(ans[i], prev - i)
13+
14+
return ans

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.