question_slug stringlengths 3 77 | title stringlengths 1 183 | slug stringlengths 12 45 | summary stringlengths 1 160 ⌀ | author stringlengths 2 30 | certification stringclasses 2
values | created_at stringdate 2013-10-25 17:32:12 2025-04-12 09:38:24 | updated_at stringdate 2013-10-25 17:32:12 2025-04-12 09:38:24 | hit_count int64 0 10.6M | has_video bool 2
classes | content stringlengths 4 576k | upvotes int64 0 11.5k | downvotes int64 0 358 | tags stringlengths 2 193 | comments int64 0 2.56k |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
length-of-longest-v-shaped-diagonal-segment | Longest V-Shaped Diagonal Path in a Grid | longest-v-shaped-diagonal-path-in-a-grid-u02x | IntuitionBrute force with dp optimizationApproachWe start at every cell with value 1 and explore in all four diagonal directions, following an alternating patte | Giri_27 | NORMAL | 2025-02-17T07:47:50.719758+00:00 | 2025-02-17T07:47:50.719758+00:00 | 8 | false | # Intuition
Brute force with dp optimization
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
We start at every cell with value 1 and explore in all four diagonal directions, following an alternating pattern between 2 and 0—allowing one change in direction—to find the longest valid V-shap... | 0 | 0 | ['C++'] | 0 |
length-of-longest-v-shaped-diagonal-segment | Simple || Beginner Friendly || DP | simple-beginner-friendly-dp-by-_phoenix_-2wxb | ApproachComplexity
Time complexity:
O(N*M)
Space complexity:
O(N*M)
Code | _Phoenix_14 | NORMAL | 2025-02-17T05:13:48.376761+00:00 | 2025-02-17T05:13:48.376761+00:00 | 8 | false | # Approach
1) Used dp(i,j,currectDirection,turnPossible)
2) Grid[ni][nj] == abs(grid[i][j]-2) to maintain the sequence 2,0,2,0... & switched 1s' to 0's before recursion to maintain the condition
3) Tried all possibilities -> 1)Maintain the same direction , 2) Turn clockwise and continue finding max length
... | 0 | 0 | ['C++'] | 0 |
length-of-longest-v-shaped-diagonal-segment | DP Memoisation : Clear explaination with choices | BEATS 100% | dp-memoisation-clear-explaination-with-c-8r6m | IntuitionApproachfor ever '1' in the grid call this function :if(grid == 1)elseOptimisation :pre-compute the diagonal length of sequence 020202... or 2020202... | SaumyaVyas | NORMAL | 2025-02-16T19:56:26.093596+00:00 | 2025-02-16T19:56:26.093596+00:00 | 57 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
for ever '1' in the grid call this function :
if(grid == 1)
explore all four direction, find which one gives best result
else
two options :
1) keep moving in same direction
2) take a turn
return which ev... | 0 | 0 | ['Dynamic Programming', 'Memoization', 'Java'] | 0 |
length-of-longest-v-shaped-diagonal-segment | Easy Brute Force C++ Solution with noturn and turn MEMO | easy-brute-force-c-solution-with-noturn-9717s | IntuitionWe use the noturn vector to record the maximum distance traveled in a single direction.
And use the turn vector to record the maximum distance with tur | william6715 | NORMAL | 2025-02-16T18:56:31.180478+00:00 | 2025-02-16T18:56:31.180478+00:00 | 10 | false | # Intuition
We use the **noturn** vector to record the maximum distance traveled in a single direction.
And use the **turn** vector to record the maximum distance with turning once.
# Approach
First, record the maximum possible distance that can be traveled from each position in the DIR direction into noturn[dir][r][c... | 0 | 0 | ['C++'] | 0 |
length-of-longest-v-shaped-diagonal-segment | DP DFS tracking process beats 100% | dp-dfs-tracking-process-beats-100-by-luu-qscp | Intuition
DP - DFS tracking process
Approach
Using ck := make([][][4]int, len(grid)) caching data
Index 0 top left, 1 top right, 2 bottom right, 3 bottom left | luudanhhieu | NORMAL | 2025-02-16T16:01:58.187906+00:00 | 2025-02-16T16:01:58.187906+00:00 | 8 | false | # Intuition
- DP - DFS tracking process
# Approach
- Using `ck := make([][][4]int, len(grid))` caching data
- Index `0 top left, 1 top right, 2 bottom right, 3 bottom left`
- Start with any point `grid[i][j] = 1` and try to calculate from all direction.
- Get result for `index i`
- Get stack for each d... | 0 | 0 | ['Go'] | 0 |
length-of-longest-v-shaped-diagonal-segment | Python Hard | python-hard-by-lucasschnee-udlp | null | lucasschnee | NORMAL | 2025-02-16T15:10:59.322216+00:00 | 2025-02-16T15:10:59.322216+00:00 | 25 | false | ```python3 []
class Solution:
def lenOfVDiagonal(self, grid: List[List[int]]) -> int:
M, N = len(grid), len(grid[0])
directions = [(1, 1), (1, -1), (-1, -1), (-1, 1)]
@cache
def calc(i, j, prev, diag_index, used):
best = 0... | 0 | 0 | ['Python3'] | 0 |
length-of-longest-v-shaped-diagonal-segment | python3 dp | python3-dp-by-maxorgus-65km | Code | MaxOrgus | NORMAL | 2025-02-16T11:59:30.507219+00:00 | 2025-02-16T11:59:30.507219+00:00 | 23 | false |
# Code
```python3 []
class Solution:
def lenOfVDiagonal(self, grid: List[List[int]]) -> int:
M = len(grid)
N = len(grid[0])
turned = {(1,1):(1,-1),(1,-1):(-1,-1),(-1,-1):(-1,1),(-1,1):(1,1)}
@cache
def dp(i,j,d,turn):
if i<0 or i>=M or j<0 or j>=N:
... | 0 | 0 | ['Dynamic Programming', 'Python3'] | 0 |
length-of-longest-v-shaped-diagonal-segment | Simple DFS | simple-dfs-by-22147407-copu | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | 22147407 | NORMAL | 2025-02-16T11:41:48.072679+00:00 | 2025-02-16T11:41:48.072679+00:00 | 11 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
`... | 0 | 0 | ['Depth-First Search', 'C++'] | 0 |
length-of-longest-v-shaped-diagonal-segment | Best C++ 3D DP Solution | best-c-3d-dp-solution-by-doitkapil-7jjy | IntuitionApproachKeep track of current position, total turns you have taken and the direction in which you are going and apply 3D DP over this.
Let me know if I | DoItKapil | NORMAL | 2025-02-16T11:36:37.332146+00:00 | 2025-02-16T11:36:37.332146+00:00 | 8 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
Keep track of current position, total turns you have taken and the direction in which you are going and apply 3D DP over this.
Let me know if I need to explain this further, happy to explain.
# Complexity
- Time complexity:
O(n... | 0 | 0 | ['C++'] | 0 |
length-of-longest-v-shaped-diagonal-segment | DFS | C++ | DP | Easy Approach | Beats 100% Space | dfs-c-dp-easy-approach-beats-100-space-b-ly9g | Code | bkbkbhavyakalra | NORMAL | 2025-02-16T10:34:52.705324+00:00 | 2025-02-16T10:36:00.487428+00:00 | 12 | false |
# Code
```cpp []
class Solution {
public:
int n;
int m;
int dx[4] = {-1,-1,1,1};
int dy[4] = {-1,1,1,-1};
int getAns(vector<vector<int>> &grid, int i, int j, int ch, int a){
int ans = 1;
if(ch == 1){
int nx = i + dx[(a + 1)%4];
int ny = j + dy[(a + 1)%4];
... | 0 | 0 | ['Dynamic Programming', 'Depth-First Search', 'Graph', 'C++'] | 0 |
length-of-longest-v-shaped-diagonal-segment | ☕ Java solution | java-solution-by-barakamon-15nz | null | Barakamon | NORMAL | 2025-02-16T09:58:05.139928+00:00 | 2025-02-16T09:58:05.139928+00:00 | 34 | false | 
```java []
public class Solution {
public int f(int[][] grid, int parent, int pi, int pj, boolean turned, int d, int n, int m) {
int pi2 = pi, pj2 = pj;
if (d == 0) {
... | 0 | 0 | ['Java'] | 0 |
length-of-longest-v-shaped-diagonal-segment | C++ | DFS + Dynamic Programming with explanation | c-dfs-dynamic-programming-with-explanati-fnqv | Ref: https://www.zerotoexpert.blog/i/157238403/problem-length-of-longest-v-shaped-diagonal-segmentSolutionIn this problem, we should find the most extended leng | a_ck | NORMAL | 2025-02-16T09:12:12.413322+00:00 | 2025-02-16T09:12:12.413322+00:00 | 8 | false | Ref: https://www.zerotoexpert.blog/i/157238403/problem-length-of-longest-v-shaped-diagonal-segment
# Solution
In this problem, we should find the most extended length that can be made with a 1,2,0,2,0 sequence. Unlike the typical DFS problem, we should move in a diagonal direction. Also, we can change the direction t... | 0 | 0 | ['C++'] | 0 |
length-of-longest-v-shaped-diagonal-segment | Travel matrix diagonally, and form dp for each of the four directions | travel-matrix-diagonally-and-form-dp-for-6m92 | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | mnnit_prakharg | NORMAL | 2025-02-16T09:06:55.640747+00:00 | 2025-02-16T09:06:55.640747+00:00 | 30 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
`... | 0 | 0 | ['Java'] | 0 |
length-of-longest-v-shaped-diagonal-segment | C++ || 6D DP -> 4D DP | c-6d-dp-4d-dp-by-neelxdxd-au64 | PLEASE UPVOTE IF YOU UNDERSTOOD
Code6D DP ~ TLEOptimized 4D DPThe new state is: dp[i][j][dirIdx][nxt/2*2 + flag]
i, j: Same as before, current cell coordinate | neelxdxd | NORMAL | 2025-02-16T09:04:47.009566+00:00 | 2025-02-16T09:07:24.385665+00:00 | 19 | false | > #### PLEASE $$UPVOTE$$ IF YOU UNDERSTOOD
# Code
**`6D DP ~ TLE`**
```cpp []
class Solution {
public:
struct VectorCompare {
bool operator()(const vector<int>& a, const vector<int>& b) const {
return a < b;
}
};
map<vector<int>,vector<int>,VectorComp... | 0 | 0 | ['Dynamic Programming', 'Recursion', 'Matrix', 'C++'] | 0 |
length-of-longest-v-shaped-diagonal-segment | Intuitive and concise DFS + memo solution | intuitive-and-concise-dfs-memo-solution-yt5c4 | IntuitionI confess that I didn't get the correct solution during the contest. This is the post correction version. I ran into a few issues that I felt tricky an | xiaozhi1 | NORMAL | 2025-02-16T08:05:15.585226+00:00 | 2025-02-16T08:13:54.686734+00:00 | 24 | false | # Intuition
I confess that I didn't get the correct solution during the contest. This is the post correction version. I ran into a few issues that I felt tricky and didn't notice while reading the question (I'm not good at reading long questions), so I refactored the code several times.
a. The turn should always be 90 ... | 0 | 0 | ['Python3'] | 0 |
length-of-longest-v-shaped-diagonal-segment | [A⭐] 312ms/100% Java: Novel A-Star-like longest possible path DFS optimization | 385ms100-java-a-like-longest-possible-pa-zy50 | Notes Before We BeginThere's no tag for A* search here, and since it's probably closest to depth-first (due to the optimization for longest possible path), I ta | mattihito | NORMAL | 2025-02-16T07:19:43.360299+00:00 | 2025-02-17T05:45:50.678826+00:00 | 35 | false | # Notes Before We Begin
There's no tag for A* search here, and since it's probably closest to depth-first (due to the optimization for longest possible path), I tagged that.
This solution is a variant of Dijkstra's algorithm optimized by a heuristic (making it more akin to A*). It's definitely not the simplest solutio... | 0 | 0 | ['Depth-First Search', 'Java'] | 0 |
length-of-longest-v-shaped-diagonal-segment | beat 100% ms | beat-100-ms-by-hoanghung3011-wrtw | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | hoanghung3011 | NORMAL | 2025-02-16T07:00:09.422560+00:00 | 2025-02-16T07:00:09.422560+00:00 | 32 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
`... | 0 | 0 | ['Java'] | 0 |
length-of-longest-v-shaped-diagonal-segment | Simple readable code. memoization | simple-readable-code-memoization-by-hari-j08u | IntuitionSeperate the logic.
first find all possible distences after clockwise 90 degree turn.
for all ones check in all four direction how for you can go. also | harisht26698 | NORMAL | 2025-02-16T06:48:32.942490+00:00 | 2025-02-16T06:48:32.942490+00:00 | 39 | false | # Intuition
Seperate the logic.
1. first find all possible distences after clockwise 90 degree turn.
2. for all ones check in all four direction how for you can go. also check max distence if you take clockwise 90 degree turn.
**Important: Clockwise turn**
# Approach
use four 2D arrays to store the max distence in on... | 0 | 0 | ['Dynamic Programming', 'Depth-First Search', 'Memoization', 'Java'] | 0 |
length-of-longest-v-shaped-diagonal-segment | Python DFS solution | python-dfs-solution-by-idntk-nhgg | IntuitionDFS and pruningCode | idntk | NORMAL | 2025-02-16T06:14:15.150464+00:00 | 2025-02-16T06:14:15.150464+00:00 | 24 | false | # Intuition
DFS and pruning
# Code
```python3 []
from functools import lru_cache
class Solution:
def lenOfVDiagonal(self, grid: List[List[int]]) -> int:
rows, cols = len(grid), len(grid[0])
states = []
memo = {}
res = 0
# Directions (diagonals):
# 0: top-left (-1,-... | 0 | 0 | ['Python3'] | 0 |
length-of-longest-v-shaped-diagonal-segment | Intuitive DFS with caching top down dp | intuitive-dfs-with-caching-top-down-dp-b-exov | ApproachDFS from valid start points to find longest possible path according to rules. Cache each state (row, column, direction, turn) to avoid repeated work.Cod | stephenip | NORMAL | 2025-02-16T06:05:01.373224+00:00 | 2025-02-16T06:05:01.373224+00:00 | 35 | false | # Approach
DFS from valid start points to find longest possible path according to rules. Cache each state (row, column, direction, turn) to avoid repeated work.
# Code
```python3 []
class Solution:
def lenOfVDiagonal(self, grid: List[List[int]]) -> int:
n = len(grid)
m = len(grid[0])
moves... | 0 | 0 | ['Dynamic Programming', 'Depth-First Search', 'Python3'] | 0 |
length-of-longest-v-shaped-diagonal-segment | Straightforward Memoization | straightforward-memoization-by-metaphysi-oa2i | IntuitionThis problem can be solved by using the typical, straightforward DP.ApproachEach DP state is (x, y, d, r, k), where (x, y) is the current cell, d is th | metaphysicalist | NORMAL | 2025-02-16T05:57:31.248527+00:00 | 2025-02-16T05:57:31.248527+00:00 | 19 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
This problem can be solved by using the typical, straightforward DP.
# Approach
<!-- Describe your approach to solving the problem. -->
Each DP state is `(x, y, d, r, k)`, where `(x, y)` is the current cell, `d` is the current direction, ... | 0 | 0 | ['Python3'] | 0 |
length-of-longest-v-shaped-diagonal-segment | DFS with memo. Beats 100%. | dfs-with-memo-beats-100-by-takasoft-cpey | Intuition and ApproachThink in terms of the pivot point where we do the clockwise 90-degree turn.Assuming we have a function dfs(row, col, direction) which retu | takasoft | NORMAL | 2025-02-16T05:14:39.145534+00:00 | 2025-02-16T05:29:13.973221+00:00 | 44 | false | # Intuition and Approach
Think in terms of the pivot point where we do the clockwise 90-degree turn.
Assuming we have a function `dfs(row, col, direction)` which returns a tuple `(count of cells that satisfy the problem condition in the given direction, whether we hit 1 at the end or not)`
At each row/col (pivot poin... | 0 | 0 | ['Depth-First Search', 'Memoization', 'Python3'] | 0 |
length-of-longest-v-shaped-diagonal-segment | PY3 | py3-by-harshitasree-i2t0 | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | harshitasree | NORMAL | 2025-02-16T04:28:04.170768+00:00 | 2025-02-16T04:28:04.170768+00:00 | 13 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
`... | 0 | 0 | ['Python3'] | 0 |
length-of-longest-v-shaped-diagonal-segment | Tedious 4D DP | tedious-4d-dp-by-whoawhoawhoa-7g9c | IntuitionLow constraints allow us to explore all 1's in the grid. But we still need to memoize the results to pass the threshold.ApproachKeeping 4D DP table for | whoawhoawhoa | NORMAL | 2025-02-16T04:24:32.024858+00:00 | 2025-02-16T04:24:32.024858+00:00 | 43 | false | # Intuition
Low constraints allow us to explore all 1's in the grid. But we still need to memoize the results to pass the threshold.
# Approach
Keeping 4D DP table for indices, direction and number of flips done (only can be 0 or 1).
Then we need to tediously keep track of patterns and boundaries.
# Complexity
- Time... | 0 | 0 | ['Dynamic Programming', 'Memoization', 'Java'] | 0 |
length-of-longest-v-shaped-diagonal-segment | O(R * C) | Top down DP | or-c-top-down-dp-by-sergey_chebotarev-lx6o | IntuitionAs usual in these types of questions, we may try and compute the answer for all grid cells if we do it without repeated calculations.If we naively try | sergey_chebotarev | NORMAL | 2025-02-16T04:24:04.740237+00:00 | 2025-02-16T04:24:04.740237+00:00 | 32 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
As usual in these types of questions, we may try and compute the answer for all grid cells if we do it without repeated calculations.
If we naively try to recursovely process every branch outgoing from every start cell, we may get a TLE e... | 0 | 0 | ['Dynamic Programming', 'Recursion', 'Memoization', 'Python3'] | 0 |
length-of-longest-v-shaped-diagonal-segment | Python Solution: Achieve About 6000ms and 700mb | python-solution-achieve-about-6000ms-and-3o5s | IntuitionThis solution use DFS (or BFS? I am not very familiar with the term). It start by exploring all possible paths from each of element 1, and avoid the me | SatriaWidy26 | NORMAL | 2025-02-16T04:23:41.943597+00:00 | 2025-02-16T04:23:41.943597+00:00 | 12 | false | # Intuition
This solution use DFS (or BFS? I am not very familiar with the term). It start by exploring all possible paths from each of element ```1```, and avoid the memory and time limit (very narrowly) by storing the explored path in a hash table (dictionary). The last part is done out of desperation, and it blows u... | 0 | 0 | ['Python3'] | 0 |
length-of-longest-v-shaped-diagonal-segment | Easy Direct Recursion Brute Force | easy-direct-recursion-brute-force-by-sir-jqb2 | IntuitionThe problem requires finding the longest V-shaped diagonal segment in a 2D grid. The segment starts with 1 and follows a sequence of 2, 0, 2, 0, etc. T | Sirjit_Saxena | NORMAL | 2025-02-16T04:11:00.836246+00:00 | 2025-02-16T04:11:00.836246+00:00 | 33 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
The problem requires finding the longest V-shaped diagonal segment in a 2D grid. The segment starts with 1 and follows a sequence of 2, 0, 2, 0, etc. The segment can make at most one 90-degree turn while maintaining the sequence. The goal i... | 0 | 0 | ['Recursion', 'C++'] | 0 |
length-of-longest-v-shaped-diagonal-segment | Brute force | brute-force-by-uurxsh19ls-qpun | The problem uses brute force instead of moving along the edges with the directions {1, 0}, {0, 1}, {-1, 0}, {0, -1}, it moves diagonally with the directions {1, | xiaochaomeng | NORMAL | 2025-02-16T04:05:51.775654+00:00 | 2025-02-16T04:05:51.775654+00:00 | 21 | false | **The problem uses brute force instead of moving along the edges with the directions {1, 0}, {0, 1}, {-1, 0}, {0, -1}, it moves diagonally with the directions {1, 1}, {-1, 1}, {-1, -1}, {1, -1}, which is the only improvement — moving diagonally**
# Code
```cpp []
class Solution {
public:
int lenOfVDiagonal(vector... | 0 | 0 | ['C++'] | 0 |
length-of-longest-v-shaped-diagonal-segment | Straightforward DP | straightforward-dp-by-jjzin-gm5u | IntuitionDFS = EZMONEYApproachStraightforward DP, keep track of current direction, whether or not you've used up your turn as well as which element is next 0 or | JJZin | NORMAL | 2025-02-16T04:04:04.157426+00:00 | 2025-02-16T04:04:04.157426+00:00 | 45 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
DFS = EZMONEY
# Approach
<!-- Describe your approach to solving the problem. -->
Straightforward DP, keep track of current direction, whether or not you've used up your turn as well as which element is next 0 or 2
# Complexity
- Time compl... | 0 | 0 | ['Python3'] | 0 |
length-of-longest-v-shaped-diagonal-segment | Intuitive DFS and precomputation || O(nm) | intuitive-dfs-and-precomputation-onm-by-ua45u | IntuitionPrecomputation:We precompute the lengths of valid sequences in four diagonal directions: up-left, up-right, down-left, and down-right. This helps in ef | aashutosh148 | NORMAL | 2025-02-16T04:03:56.767683+00:00 | 2025-02-16T04:03:56.767683+00:00 | 106 | false | # Intuition
Precomputation:
We precompute the lengths of valid sequences in four diagonal directions: up-left, up-right, down-left, and down-right. This helps in efficiently calculating the length of sequences when a turn is taken.
DFS Traversal:
For each cell containing 1, we perform a DFS in all four diagonal dire... | 0 | 0 | ['Depth-First Search', 'Matrix', 'C++'] | 1 |
length-of-longest-v-shaped-diagonal-segment | bruteforce | bruteforce-by-akbc-o6g2 | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | akbc | NORMAL | 2025-02-16T04:01:42.261716+00:00 | 2025-02-16T04:01:42.261716+00:00 | 31 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
`... | 0 | 0 | ['Python3'] | 0 |
shortest-distance-to-a-character | [C++/Java/Python] 2-Pass with Explanation | cjavapython-2-pass-with-explanation-by-l-w2de | Solution 1: Record the Position\n\nInitial result array.\nLoop twice on the string S.\nFirst forward pass to find shortest distant to character on left.\nSecond | lee215 | NORMAL | 2018-04-22T03:02:07.605639+00:00 | 2020-02-03T17:21:00.519833+00:00 | 39,974 | false | # Solution 1: Record the Position\n\nInitial result array.\nLoop twice on the string `S`.\nFirst forward pass to find shortest distant to character on left.\nSecond backward pass to find shortest distant to character on right.\n<br>\n\nIn python solution, I merged these two `for` statement.\nWe can do the same in C++/... | 412 | 3 | [] | 47 |
shortest-distance-to-a-character | Concise java solution with detailed explanation. Easy understand!!! | concise-java-solution-with-detailed-expl-3k3v | ```\n/* "loveleetcode" "e"\n * 1. put 0 at all position equals to e, and max at all other position\n * we will get [max, max, max, 0, max, 0, 0, max, max, | self_learner | NORMAL | 2018-04-24T06:07:54.749986+00:00 | 2018-09-04T23:45:30.816157+00:00 | 6,082 | false | ```\n/** "loveleetcode" "e"\n * 1. put 0 at all position equals to e, and max at all other position\n * we will get [max, max, max, 0, max, 0, 0, max, max, max, max, 0]\n * 2. scan from left to right, if =max, skip, else dist[i+1] = Math.min(dp[i] + 1, dp[i+1]), \n * we can get [max, max, max, 0, 1, 0, 0, 1, ... | 63 | 1 | [] | 8 |
shortest-distance-to-a-character | C++ | Two Pass | O(n), 0ms, Beats 100% | Easy Explanation | c-two-pass-on-0ms-beats-100-easy-explana-so05 | EXPLANATION\n- First, iterate the string \'s\' and store the indexes of \'c\' present in \'s\' into an array or vector ( here vector<int>ioc ) .\n- Make a left | akash2099 | NORMAL | 2021-02-07T11:48:08.601560+00:00 | 2021-02-07T12:19:53.128278+00:00 | 5,296 | false | **EXPLANATION**\n- First, iterate the string **\'s\'** and store the **indexes** of **\'c\'** present in \'s\' into an array or vector ( here **```vector<int>ioc```** ) .\n- Make a **left** variable for storing the index of **left nearest \'c\'** in **```ioc```** and a **right** variable for storing the index of **rig... | 51 | 3 | ['C'] | 7 |
shortest-distance-to-a-character | [javascript] 2 pass simple solution with explanation | javascript-2-pass-simple-solution-with-e-pyqd | The problem becomes really simple if we consider the example.\njs\n0 1 2 3 4 5 6 7 8 9 10 11\nl o v e l e e t c o d e\n\nWhat is the shortest distance for inde | bt4r9 | NORMAL | 2021-02-07T10:57:02.200229+00:00 | 2021-02-08T08:27:14.818458+00:00 | 1,973 | false | The problem becomes really simple if we consider the example.\n```js\n0 1 2 3 4 5 6 7 8 9 10 11\nl o v e l e e t c o d e\n```\nWhat is the shortest distance for index `9` if the character is `e`?\nThe closest `e` from the left side has the index `6`.\n`9 - 6 = 3`\nThe closest `e` from the right side has the index `11`... | 31 | 0 | ['JavaScript'] | 1 |
shortest-distance-to-a-character | [Python] O(n) solution, explained | python-on-solution-explained-by-dbabiche-xhrk | What we need to do in this problem is to iterate our data two times: one time from left to right and second time from right to left. Let us use auxilary functio | dbabichev | NORMAL | 2021-02-07T10:14:47.353528+00:00 | 2021-02-08T08:17:31.209152+00:00 | 3,024 | false | What we need to do in this problem is to iterate our data two times: one time from left to right and second time from right to left. Let us use auxilary function `letter_get(letter, dr)`, where `dr` is direction: `+1` for left->right traversal and `-1` for right -> left traversal.\n\nHow this function will work? We ini... | 30 | 1 | [] | 4 |
shortest-distance-to-a-character | C++ Simple Solution 0 ms faster than 100% | c-simple-solution-0-ms-faster-than-100-b-f5r0 | \nclass Solution {\npublic:\n vector<int> shortestToChar(string s, char c) {\n vector<int> res;\n int prev_char = -s.size();\n for (int | yehudisk | NORMAL | 2021-02-07T08:56:21.290683+00:00 | 2021-02-07T09:12:18.036364+00:00 | 1,739 | false | ```\nclass Solution {\npublic:\n vector<int> shortestToChar(string s, char c) {\n vector<int> res;\n int prev_char = -s.size();\n for (int i = 0; i < s.size(); i++) {\n if (s[i] == c)\n prev_char = i;\n res.push_back(i - prev_char);\n }\n\n for ... | 22 | 2 | ['C'] | 2 |
shortest-distance-to-a-character | ✅C++ solution || Simple || Easy-understanding | c-solution-simple-easy-understanding-by-kabq1 | Explanation:\n create two vectors :- position and answer.\n Traverse the string and collect all the position of given char using the position vector.\n Now trav | ayushsenapati123 | NORMAL | 2022-06-20T08:16:17.474223+00:00 | 2022-06-20T08:16:17.474262+00:00 | 2,754 | false | **Explanation:**\n* create two vectors :- `position` and `answer`.\n* Traverse the string and collect all the position of given char using the `position` vector.\n* Now traverse the string and find the shortest distance from the given char to all given positions.\n* Keep pushing the distance to the `answer` vector and ... | 21 | 0 | ['C', 'C++'] | 1 |
shortest-distance-to-a-character | Python O(n) by propagation 85%+ [w/ Diagram] | python-on-by-propagation-85-w-diagram-by-i8mv | Python O(n) by propagation\n\n---\n\nHint:\n\nImagine parameter C as a flag on the line.\n\nThink of propagation technique:\n1st-pass iteration propagates dista | brianchiang_tw | NORMAL | 2020-03-03T03:13:33.691717+00:00 | 2023-12-18T09:29:25.181152+00:00 | 2,572 | false | Python O(n) by propagation\n\n---\n\n**Hint**:\n\nImagine parameter C as a flag on the line.\n\nThink of propagation technique:\n**1st-pass** iteration **propagates distance** from C on the **left hand side**\n**2nd-pass** iteration **propagates distance** from C on the **right hand side** with min( 1st-pass result, 2n... | 20 | 1 | ['Python', 'Python3'] | 4 |
shortest-distance-to-a-character | Python 3 | python-3-by-zychen016-exu7 | \nclass Solution:\n def shortestToChar(self, S, C):\n """\n :type S: str\n :type C: str\n :rtype: List[int]\n """\n | zychen016 | NORMAL | 2018-05-17T13:05:34.434464+00:00 | 2018-10-25T14:39:35.628981+00:00 | 3,486 | false | ```\nclass Solution:\n def shortestToChar(self, S, C):\n """\n :type S: str\n :type C: str\n :rtype: List[int]\n """\n c = []\n for i, v in enumerate(S):\n if v == C:\n c.append(i)\n\n r = []\n for i in range(len(S)):\n ... | 17 | 3 | [] | 6 |
shortest-distance-to-a-character | Python || 99.92% Faster || Two Pointers || O(n) Solution | python-9992-faster-two-pointers-on-solut-dgnu | \nclass Solution:\n def shortestToChar(self, s: str, c: str) -> List[int]:\n a,n=[],len(s)\n for i in range(n):\n if s[i]==c:\n | pulkit_uppal | NORMAL | 2022-11-27T15:24:08.175101+00:00 | 2022-11-27T15:25:18.127808+00:00 | 4,882 | false | ```\nclass Solution:\n def shortestToChar(self, s: str, c: str) -> List[int]:\n a,n=[],len(s)\n for i in range(n):\n if s[i]==c:\n a.append(i)\n answer=[]\n j=0\n for i in range(n):\n if s[i]==c:\n answer.append(0)\n ... | 16 | 1 | ['Array', 'Two Pointers', 'Python', 'Python3'] | 5 |
shortest-distance-to-a-character | java 98% 100% | java-98-100-by-themonkey-g6u7 | \nclass Solution {\n public int[] shortestToChar(String S, char C) {\n char[] arrS=S.toCharArray(); \n int[] dist=new int[arrS.length];\n | themonkey | NORMAL | 2019-07-26T05:27:54.416465+00:00 | 2019-07-26T05:27:54.416498+00:00 | 1,707 | false | ```\nclass Solution {\n public int[] shortestToChar(String S, char C) {\n char[] arrS=S.toCharArray(); \n int[] dist=new int[arrS.length];\n int disToL=S.length(), disToR=S.length(); \n \n for(int i=0;i<arrS.length;i++){ //pass 1, determine distance to nearest C on the left \n ... | 16 | 0 | [] | 7 |
shortest-distance-to-a-character | Java - Single Pass with Trailing Pointer (Concise) | java-single-pass-with-trailing-pointer-c-tmj3 | Idea is exactly the same as other solutions using stack or two pointers. Keep track of the last seen target character C as well as a left pointer pointing to th | bradshaw | NORMAL | 2018-04-22T05:19:14.949470+00:00 | 2018-09-26T23:19:23.759964+00:00 | 3,509 | false | Idea is exactly the same as other solutions using stack or two pointers. Keep track of the last seen target character C as well as a left pointer pointing to the last non C character. \n\nIf you hit a nonC character and have not seen any C yet, max out that value.\n\nIf you hit a nonC character and have seen a C, the c... | 16 | 2 | [] | 7 |
shortest-distance-to-a-character | Intuition leads to approach | Java | optimize intuition to get effective solution | intuition-leads-to-approach-java-optimiz-0e13 | Intuition:\nSimple intuition: \n\n Suppose the answer is stored ans array.\n ans[i] is minimum of distances from all the positions of character C in string S\nE | ksumit9895 | NORMAL | 2021-02-08T10:50:00.044735+00:00 | 2021-02-08T10:50:00.044764+00:00 | 1,715 | false | **Intuition:**\n**Simple intuition**: \n\n* Suppose the answer is stored ans array.\n* ans[i] is minimum of distances from all the positions of character C in string S\nEx : Input: s = "loveleetcode", c = "e"\nIndices of e = {3, 5, 6, 12}\n* To get the shortest distance, we need to check the nearest from both sides.\n*... | 11 | 0 | ['Java'] | 3 |
shortest-distance-to-a-character | [JAVA] BEATS 100.00% MEMORY/SPEED 0ms // APRIL 2022 | java-beats-10000-memoryspeed-0ms-april-2-j7rf | \n\tclass Solution {\n\n public int[] shortestToChar(String s, char c) {\n int len = s.length();\n int ans[] = new int[len];\n int prev | darian-catalin-cucer | NORMAL | 2022-04-20T06:40:56.250241+00:00 | 2022-04-20T06:40:56.250289+00:00 | 1,723 | false | \n\tclass Solution {\n\n public int[] shortestToChar(String s, char c) {\n int len = s.length();\n int ans[] = new int[len];\n int prev = len;\n \n // forward\n for(int i = 0; i < len; i++){\n if(s.charAt(i) == c){\n prev = 0;\n ans[i... | 10 | 0 | ['Java'] | 1 |
shortest-distance-to-a-character | Shortest Distance to a Character | Simple DP Solution w/ Explanation | beats 100% / 100% | shortest-distance-to-a-character-simple-0y0y3 | (Note: This is part of a series of Leetcode solution explanations (index). If you like this solution or find it useful, please upvote this post.)\n\n---\n\nIdea | sgallivan | NORMAL | 2021-02-07T09:13:40.649470+00:00 | 2021-02-07T09:13:40.649508+00:00 | 410 | false | *(Note: This is part of a series of Leetcode solution explanations ([**index**](https://dev.to/seanpgallivan/leetcode-solutions-index-57fl)). If you like this solution or find it useful,* ***please upvote*** *this post.)*\n\n---\n\n***Idea:***\n\nSince this problem is asking us to reference characters both ahead and be... | 10 | 6 | [] | 2 |
shortest-distance-to-a-character | easy solution | easy-solution-by-leelasravanthi423-161i | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | leelasravanthi423 | NORMAL | 2023-12-13T17:34:19.704081+00:00 | 2023-12-13T17:34:19.704113+00:00 | 1,068 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 9 | 0 | ['Python3'] | 0 |
shortest-distance-to-a-character | C++ || Basic || Easy implementaion | c-basic-easy-implementaion-by-priyamesh2-k1bn | \nclass Solution {\npublic:\n vector<int> shortestToChar(string s, char c) {\n vector<int>out;\n vector<int>pos;\n \n //collect a | priyamesh28 | NORMAL | 2021-06-11T09:02:43.965760+00:00 | 2021-06-11T09:02:43.965804+00:00 | 640 | false | ```\nclass Solution {\npublic:\n vector<int> shortestToChar(string s, char c) {\n vector<int>out;\n vector<int>pos;\n \n //collect all the position of given char\n for(int i=0;i<s.size();i++)\n {\n if(s[i]==c)\n pos.push_back(i);\n }\n ... | 9 | 0 | ['C'] | 0 |
shortest-distance-to-a-character | JavaScript Simple 1-Pass Beats 100% | javascript-simple-1-pass-beats-100-by-co-jljv | javascript\nvar shortestToChar = function(s, c) {\n const answer = Array(s.length).fill(Infinity);\n let l = Infinity, r = Infinity;\n \n for(let f | control_the_narrative | NORMAL | 2021-02-07T08:46:03.364078+00:00 | 2021-02-08T10:28:19.712931+00:00 | 804 | false | ```javascript\nvar shortestToChar = function(s, c) {\n const answer = Array(s.length).fill(Infinity);\n let l = Infinity, r = Infinity;\n \n for(let f = 0; f < s.length; f++) {\n const b = s.length-1-f;\n \n l = s[f] === c ? 0 : l+1;\n r = s[b] === c ? 0 : r+1;\n \n ... | 9 | 0 | ['JavaScript'] | 4 |
shortest-distance-to-a-character | [Java], 2 Solutions, self explained, easy to read, 2 iterations | java-2-solutions-self-explained-easy-to-dx1uu | 1- Using DP\n going from left to right, at each position i ans[i] = ans[i-1] + 1\n going from right to left, at each position i ans[i] = ans[i+1] + 1\n\npublic | ahmednabil | NORMAL | 2021-02-07T11:05:06.404545+00:00 | 2021-02-07T12:38:42.604567+00:00 | 1,353 | false | 1- Using DP\n* going from left to right, at each position i ans[i] = ans[i-1] + 1\n* going from right to left, at each position i ans[i] = ans[i+1] + 1\n```\npublic int[] shortestToChar(String s, char c) {\n int n = s.length();\n int[] ans = new int[n];\n Arrays.fill(ans, n);\n for(int i=0; ... | 8 | 0 | ['Java'] | 0 |
shortest-distance-to-a-character | Beginner friendly easy solution | JAVA | beginner-friendly-easy-solution-java-by-u4y9o | \n\n# Code\n\nclass Solution {\n public int[] shortestToChar(String s, char c) {\n int[] ans=new int[s.length()];\n List<Integer> ls=new ArrayL | sumo25 | NORMAL | 2024-02-09T12:29:42.168451+00:00 | 2024-02-09T12:29:42.168479+00:00 | 907 | false | \n\n# Code\n```\nclass Solution {\n public int[] shortestToChar(String s, char c) {\n int[] ans=new int[s.length()];\n List<Integer> ls=new ArrayList<>();\n for(int i=0;i<s.length();i++){\n if(s.charAt(i)==c){\n ls.add(i);\n }\n }\n \n fo... | 7 | 0 | ['Java'] | 0 |
shortest-distance-to-a-character | Python 3 Solution, Brute Force and Two Pointers - 2 Solutions | python-3-solution-brute-force-and-two-po-rr73 | Brute Force:\n\nclass Solution:\n def shortestToChar(self, s: str, c: str) -> List[int]:\n req = []\n ind_list = []\n for i in range(len | deleted_user | NORMAL | 2022-05-04T08:18:05.542540+00:00 | 2022-05-04T08:18:05.542569+00:00 | 1,224 | false | Brute Force:\n```\nclass Solution:\n def shortestToChar(self, s: str, c: str) -> List[int]:\n req = []\n ind_list = []\n for i in range(len(s)):\n if s[i] == c:\n ind_list.append(i)\n min_dis = len(s)\n for j in range(len(s)):\n for k in range(l... | 7 | 0 | ['Two Pointers', 'Python', 'Python3'] | 2 |
shortest-distance-to-a-character | JavaScript - 2 passes | javascript-2-passes-by-slkuo230-jfo7 | \nvar shortestToChar = function(S, C) {\n const dp = new Array(S.length).fill(Infinity);\n \n dp[0] = S[0] === C ? 0 : Infinity\n \n for(let i = | slkuo230 | NORMAL | 2020-02-16T21:35:29.139224+00:00 | 2020-02-16T21:35:29.139258+00:00 | 953 | false | ```\nvar shortestToChar = function(S, C) {\n const dp = new Array(S.length).fill(Infinity);\n \n dp[0] = S[0] === C ? 0 : Infinity\n \n for(let i = 1; i < S.length; i++) {\n if(S[i] === C) {\n dp[i] = 0;\n } else {\n dp[i] = dp[i-1] === Infinity ? Infinity : dp[i-1] + ... | 7 | 0 | ['JavaScript'] | 1 |
shortest-distance-to-a-character | c++ easy solution | c-easy-solution-by-sailakshmi1-qws5 | ```\nclass Solution {\npublic:\n vector shortestToChar(string s, char c) {\n vectorposition;\n vectorans;\n for(int i=0;i<s.length();i++ | sailakshmi1 | NORMAL | 2022-06-19T13:30:36.332170+00:00 | 2022-06-19T13:30:36.332217+00:00 | 833 | false | ```\nclass Solution {\npublic:\n vector<int> shortestToChar(string s, char c) {\n vector<int>position;\n vector<int>ans;\n for(int i=0;i<s.length();i++){\n if(s[i]==c){\n position.push_back(i);\n }\n }\n for(int i=0;i<s.length();i++){ \n ... | 6 | 0 | ['C', 'C++'] | 0 |
shortest-distance-to-a-character | 【Python3】Any improvement ? | python3-any-improvement-by-qiaochow-00rz | \nclass Solution:\n def shortestToChar(self, s: str, c: str) -> List[int]:\n L = []\n for idx, value in enumerate(s):\n if value == | qiaochow | NORMAL | 2021-05-23T22:46:47.533774+00:00 | 2021-05-23T22:46:47.533817+00:00 | 780 | false | ```\nclass Solution:\n def shortestToChar(self, s: str, c: str) -> List[int]:\n L = []\n for idx, value in enumerate(s):\n if value == c:\n L.append(idx)\n \n distance = []\n i = 0\n for idx, value in enumerate(s):\n if value == c:\n ... | 6 | 0 | ['Python', 'Python3'] | 0 |
shortest-distance-to-a-character | [Java] | Two Pointers | One Pass | Time: O(n) | Optimal - Faster than 98.5% | java-two-pointers-one-pass-time-on-optim-6gud | Approach:\n\nMaintain two pointers (one for the current character in S (sIndex) and the other (cIndex) to track the next occurence of C in S). Also have another | svantedd | NORMAL | 2020-06-14T19:44:59.566027+00:00 | 2020-06-14T19:51:55.728544+00:00 | 374 | false | Approach:\n\nMaintain two pointers (one for the current character in S (sIndex) and the other (cIndex) to track the next occurence of C in S). Also have another variable to hold the previous occurence of C in S (prevCIndex) which is initially set to -1.\n\nFirstly, as it\'s guaranteed to have at least one C in S, find ... | 6 | 1 | ['Two Pointers'] | 1 |
shortest-distance-to-a-character | JavaScript Solution with Comments | javascript-solution-with-comments-by-spo-oybn | Runtime: 92 ms, faster than 51.38% of JavaScript online submissions\n> Memory Usage: 40.8 MB, less than 58.84% of JavaScript online submissions\n\njavascript\n/ | sporkyy | NORMAL | 2020-02-20T13:53:57.962598+00:00 | 2021-03-05T21:26:15.427659+00:00 | 743 | false | > Runtime: **92 ms**, faster than *51.38%* of JavaScript online submissions\n> Memory Usage: **40.8 MB**, less than *58.84%* of JavaScript online submissions\n\n```javascript\n/**\n * @param {string} s\n * @param {character} c\n * @return {number[]}\n */\nconst shortestToChar = (s, c) => {\n // Create an array to hold... | 6 | 0 | ['JavaScript'] | 1 |
shortest-distance-to-a-character | Kotlin Best Solution | kotlin-best-solution-by-progp-de4d | \n# Code\n\nimport kotlin.math.min\n\nclass Solution {\n fun shortestToChar(s: String, c: Char): IntArray {\n\t// tracks postitions of occurences of the char | progp | NORMAL | 2023-12-10T08:00:28.451876+00:00 | 2023-12-10T08:00:28.451914+00:00 | 190 | false | \n# Code\n```\nimport kotlin.math.min\n\nclass Solution {\n fun shortestToChar(s: String, c: Char): IntArray {\n\t// tracks postitions of occurences of the character c in string s\n var positions = mutableListOf<Int>()\n\t\t\n var ans = mutableListOf<Int>()\n \n for(i in 0..s.length-1){\n... | 5 | 0 | ['Kotlin'] | 0 |
shortest-distance-to-a-character | Java | Do check out for explanation | Faster than 100% | java-do-check-out-for-explanation-faster-thsb | Do vote up if you like it :)\n\nThe idea is to find the minimum char say c index from right for all position.\nAnd again find the minimum char say c index from | nknidhi321 | NORMAL | 2021-06-27T09:11:34.391146+00:00 | 2021-06-27T09:14:10.184176+00:00 | 195 | false | **Do vote up if you like it :)**\n\nThe idea is to find the minimum char say c index from right for all position.\nAnd again find the minimum char say c index from left for all position.\nNow, whichever (left or right) gives you the minimum that is the minimum distance for char c for that position.\n\n```\nclass Soluti... | 5 | 0 | [] | 0 |
shortest-distance-to-a-character | 2-line python solution | 2-line-python-solution-by-mhviraf-zq88 | \nclass Solution:\n def shortestToChar(self, s: str, c: str) -> List[int]:\n ids = [i for i in range(len(s)) if s[i] == c]\n return [min([abs(i | mhviraf | NORMAL | 2021-03-03T06:07:31.955173+00:00 | 2021-03-03T06:07:31.955230+00:00 | 855 | false | ```\nclass Solution:\n def shortestToChar(self, s: str, c: str) -> List[int]:\n ids = [i for i in range(len(s)) if s[i] == c]\n return [min([abs(i-id_) for id_ in ids]) for i in range(len(s))]\n``` | 5 | 0 | ['Python', 'Python3'] | 0 |
shortest-distance-to-a-character | [C++] [Two Pass] - Easy to understand solution | c-two-pass-easy-to-understand-solution-b-uvq7 | Approach 1 : Using BFS for shortest distance \n\tTime Complexity - O(n)\n\tSpace Complexity - O(n) \n\nclass Solution {\npublic:\n vector<int> shortestToChar | morning_coder | NORMAL | 2021-02-08T03:57:10.600634+00:00 | 2021-02-08T03:59:21.005026+00:00 | 801 | false | **Approach 1 : Using BFS for shortest distance** \n\tTime Complexity - O(n)\n\tSpace Complexity - O(n) \n```\nclass Solution {\npublic:\n vector<int> shortestToChar(string s, char c) {\n int len=s.length();\n vector<int> ans(len,INT_MAX);\n queue<vector<int> > q;\n for(int i=0;i<len;i++)\... | 5 | 0 | ['Breadth-First Search', 'C', 'C++'] | 0 |
shortest-distance-to-a-character | 2-pass but different and intuitive; comments included | 2-pass-but-different-and-intuitive-comme-kgt2 | \nimport kotlin.math.min\n\nclass Solution {\n fun shortestToChar(s: String, c: Char): IntArray {\n\t// tracks postitions of occurences of the character c in | rakeshseervi | NORMAL | 2021-02-07T10:56:01.634215+00:00 | 2021-02-07T11:29:17.037390+00:00 | 271 | false | ```\nimport kotlin.math.min\n\nclass Solution {\n fun shortestToChar(s: String, c: Char): IntArray {\n\t// tracks postitions of occurences of the character c in string s\n var positions = mutableListOf<Int>()\n\t\t\n var ans = mutableListOf<Int>()\n \n for(i in 0..s.length-1){\n ... | 5 | 0 | ['Kotlin'] | 1 |
shortest-distance-to-a-character | Python. O(n), Simple & easy-understanding cool solution. | python-on-simple-easy-understanding-cool-s30b | \tclass Solution:\n\t\tdef shortestToChar(self, s: str, c: str) -> List[int]:\n\t\t\tn = lastC =len(s)\n\t\t\tans = [n] * n\n\t\t\tfor i in itertools.chain(rang | m-d-f | NORMAL | 2021-02-07T08:31:01.957605+00:00 | 2021-02-07T08:59:03.191157+00:00 | 487 | false | \tclass Solution:\n\t\tdef shortestToChar(self, s: str, c: str) -> List[int]:\n\t\t\tn = lastC =len(s)\n\t\t\tans = [n] * n\n\t\t\tfor i in itertools.chain(range(n), range(n)[::-1]):\n\t\t\t\tif s[i] == c: lastC = i\n\t\t\t\tans[i] = min(ans[i], abs( i - lastC))\n\t\t\treturn ans | 5 | 2 | ['Python', 'Python3'] | 0 |
shortest-distance-to-a-character | Python One Line Solution!!! | python-one-line-solution-by-debbiealter-tm6k | \nclass Solution:\n def shortestToChar(self, s: str, c: str) -> List[int]:\n return [min([abs(j - i) for j in [i for i in range(len(s)) if s[i] == c]] | debbiealter | NORMAL | 2021-02-07T08:06:16.457251+00:00 | 2021-02-07T08:06:35.302178+00:00 | 224 | false | ```\nclass Solution:\n def shortestToChar(self, s: str, c: str) -> List[int]:\n return [min([abs(j - i) for j in [i for i in range(len(s)) if s[i] == c]]) for i in range(len(s))]\n``` | 5 | 4 | ['Python'] | 4 |
shortest-distance-to-a-character | Python3 100% faster, 100% less memory (20ms, 14mb) | python3-100-faster-100-less-memory-20ms-btts9 | \nclass Solution:\n def shortestToChar(self, S: str, C: str) -> List[int]:\n l = [0] * len(S)\n prev = None\n for i, x in enumerate(S):\ | haasosaurus | NORMAL | 2020-10-29T05:46:49.369088+00:00 | 2020-10-29T06:10:05.758016+00:00 | 1,099 | false | ```\nclass Solution:\n def shortestToChar(self, S: str, C: str) -> List[int]:\n l = [0] * len(S)\n prev = None\n for i, x in enumerate(S):\n if x == C:\n\n\t\t\t\t# only correct the closer half of the indexes between previous and current if previous is not None\n start ... | 5 | 0 | ['Python', 'Python3'] | 1 |
shortest-distance-to-a-character | Sharing Most Elegant cpp solution [ 12 ms ] | sharing-most-elegant-cpp-solution-12-ms-o3fun | ```\nvector shortestToChar(string S, char C) {\n sets; int currentMin(INT_MAX);vectorans{};\n for(int i=0;i<S.length();i++) if(S[i]==C) s.insert(i | sarora160 | NORMAL | 2020-07-10T12:23:01.940867+00:00 | 2020-07-10T12:23:01.940916+00:00 | 311 | false | ```\nvector<int> shortestToChar(string S, char C) {\n set<int>s; int currentMin(INT_MAX);vector<int>ans{};\n for(int i=0;i<S.length();i++) if(S[i]==C) s.insert(i);\n for(int i=0;i<S.length();i++){\n for(auto x:s){\n if(abs(i-x)<currentMin) currentMin=abs(i-x);\n ... | 5 | 3 | ['C', 'C++'] | 2 |
shortest-distance-to-a-character | C++ Simplest Solution O(n) | c-simplest-solution-on-by-ddev-m4mp | vector<int> shortestToChar(string S, char C) {\n int len = S.length(), loc = -1;\n vector<int> result(len, INT_MAX);\n \n for(int i | ddev | NORMAL | 2018-04-22T03:04:13.760593+00:00 | 2018-04-22T03:04:13.760593+00:00 | 1,589 | false | vector<int> shortestToChar(string S, char C) {\n int len = S.length(), loc = -1;\n vector<int> result(len, INT_MAX);\n \n for(int i = 0; i < len; i++) {\n if(S[i] == C) loc = i;\n result[i] = min(result[i], loc != -1 ? abs(i - loc) : INT_MAX);\n }\n \n... | 5 | 2 | [] | 0 |
shortest-distance-to-a-character | Simple solution using two pointers | O(N) | 1ms | simple-solution-using-two-pointers-on-1m-l0yt | Intuition\n Describe your first thoughts on how to solve this problem. \n- Using two pointer approach\n\n# Approach\n Describe your approach to solving the prob | astronag | NORMAL | 2023-08-12T12:56:45.065235+00:00 | 2023-08-12T12:59:09.810024+00:00 | 1,033 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n- Using two pointer approach\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- We need to store previous and next closest occurrence of the character `c` in both the sides of the current index, `i`.\n- `previous` w... | 4 | 0 | ['Two Pointers', 'Java'] | 0 |
shortest-distance-to-a-character | ✌️😀 😀 JAVA EASY TO UNDERSTAND 😀 😀 ✌️ | java-easy-to-understand-by-sauravmehta-b2ql | Happy Coding\u270C\uFE0F\u270C\uFE0F\u270C\uFE0F\n\nclass Solution {\n public int[] shortestToChar(String s, char c)\n {\n int[] arr=new int[s.length() | Sauravmehta | NORMAL | 2022-10-10T13:55:39.193903+00:00 | 2022-10-10T13:55:39.193942+00:00 | 1,869 | false | # Happy Coding\u270C\uFE0F\u270C\uFE0F\u270C\uFE0F\n```\nclass Solution {\n public int[] shortestToChar(String s, char c)\n {\n int[] arr=new int[s.length()];\n for(int i=0;i<s.length();i++)\n {\n int left=i-1;\n int right=i+1;\n while(left>=0 || right <s.length()... | 4 | 0 | ['Java'] | 0 |
shortest-distance-to-a-character | C++ | Easiest | O(n) time | 100% fastest | c-easiest-on-time-100-fastest-by-samarpi-vrgi | \n vector<int> shortestToChar(string s, char c) {\n vector<int> v , ans;\n for(int i = 0 ; i < s.size() ;i++){\n if(s[i] == c)\n | samarpitdua24 | NORMAL | 2022-09-19T10:00:14.618579+00:00 | 2022-09-19T10:00:43.299344+00:00 | 717 | false | ```\n vector<int> shortestToChar(string s, char c) {\n vector<int> v , ans;\n for(int i = 0 ; i < s.size() ;i++){\n if(s[i] == c)\n v.push_back(i);\n }\n int j = 0;\n for(int i = 0 ; i < s.size() ;i++){\n \n if(j == 0)\n a... | 4 | 0 | ['C', 'C++'] | 0 |
shortest-distance-to-a-character | Easy Java O(N) soution | easy-java-on-soution-by-anup-08ao | \npublic static int[] shortestToChar(String s, char c) {\n int n = s.length();\n int[] arr = new int[n];\n int c_position = -n;\n\n | anup_ | NORMAL | 2022-09-10T10:28:11.736083+00:00 | 2022-09-10T10:28:11.736125+00:00 | 1,184 | false | ```\npublic static int[] shortestToChar(String s, char c) {\n int n = s.length();\n int[] arr = new int[n];\n int c_position = -n;\n\n for (int i = 0; i < n; i++) {\n if(s.charAt(i) == c){\n c_position = i;\n }\n arr[i] = i - c_position;\n ... | 4 | 0 | ['Java'] | 0 |
shortest-distance-to-a-character | Easiest Python solution with explanation | easiest-python-solution-with-explanation-grqw | \nclass Solution:\n def shortestToChar(self, s: str, c: str) -> List[int]:\n # occurence of charachter in the array.\n occ = []\n for i | EbrahimMG | NORMAL | 2022-07-02T10:43:02.918004+00:00 | 2022-07-02T10:43:02.918047+00:00 | 801 | false | ```\nclass Solution:\n def shortestToChar(self, s: str, c: str) -> List[int]:\n # occurence of charachter in the array.\n occ = []\n for i in range(len(s)):\n if s[i] == c:\n occ.append(i)\n ans = []\n for i in range(len(s)):\n #checking distanc... | 4 | 0 | ['Array', 'Python', 'Python3'] | 0 |
shortest-distance-to-a-character | [PYTHON] Shortest Distance to a Character | Runtime: 32ms & Memory usage: 14,200 kB | python-shortest-distance-to-a-character-4an29 | """\n implemented by me \n Noted: Time Complexity = 32ms O(n)\n Space Complexity = O(n)\n """\n\n dist = []\n | arunrathi201 | NORMAL | 2021-02-08T07:41:56.428799+00:00 | 2021-02-08T07:41:56.428868+00:00 | 264 | false | """\n implemented by me \n Noted: Time Complexity = 32ms O(n)\n Space Complexity = O(n)\n """\n\n dist = []\n c_ind = []\n for i in range(len(s)):\n if s[i] == c:\n c_ind.append(i)\n\n c_count = 0\n for i in range(len(s)):\... | 4 | 1 | ['Python'] | 0 |
shortest-distance-to-a-character | Java || 2-pointer || O(n) || 1ms || beats 97% | java-2-pointer-on-1ms-beats-97-by-legend-xmxf | \n public int[] shortestToChar(String s, char c) {\n \n int ptr1 = Integer.MAX_VALUE;\n int ptr2 = Integer.MAX_VALUE;\n int index | LegendaryCoder | NORMAL | 2021-02-07T11:49:45.864046+00:00 | 2021-02-07T11:51:21.992569+00:00 | 294 | false | \n public int[] shortestToChar(String s, char c) {\n \n int ptr1 = Integer.MAX_VALUE;\n int ptr2 = Integer.MAX_VALUE;\n int index = -1;\n int l = s.length();\n int[] ans = new int[l];\n \n while(index<l-1){\n int temp = index+1;\n \n ... | 4 | 0 | [] | 0 |
shortest-distance-to-a-character | C++. O(n), Simple & easy-understanding cool solution. | c-on-simple-easy-understanding-cool-solu-235j | \tclass Solution {\n\tpublic:\n\t\tvector shortestToChar(string s, char c) {\n\t\t\tint n = s.size(), lastC = -n, i = 0;\n\t\t\tvector ans (n, n);\n\t\t\tfor (; | m-d-f | NORMAL | 2021-02-07T08:55:09.667808+00:00 | 2021-02-07T08:56:33.623465+00:00 | 409 | false | \tclass Solution {\n\tpublic:\n\t\tvector<int> shortestToChar(string s, char c) {\n\t\t\tint n = s.size(), lastC = -n, i = 0;\n\t\t\tvector<int> ans (n, n);\n\t\t\tfor (; i < n; ++i)\n\t\t\t{\n\t\t\t\tif ( s[i] == c ) lastC = i;\n\t\t\t\tans[i] = i - lastC;\n\t\t\t}\n\t\t\tfor (i = lastC; i >= 0; --i)\n\t\t\t{\n\t\t\t\... | 4 | 0 | ['C', 'C++'] | 0 |
shortest-distance-to-a-character | 2 Solutions | Easy to Understand | Faster | Binary Search | Python Solution | 2-solutions-easy-to-understand-faster-bi-q8b2 | \n def using_solution_tab_solution(self, S, C):\n out = []\n prev = float(\'inf\')\n for i, v in enumerate(S):\n if v == C: p | mrmagician | NORMAL | 2020-05-01T03:26:08.524729+00:00 | 2020-05-01T03:26:08.524766+00:00 | 845 | false | ```\n def using_solution_tab_solution(self, S, C):\n out = []\n prev = float(\'inf\')\n for i, v in enumerate(S):\n if v == C: prev = i\n out.append(abs(prev - i))\n prev = float(\'inf\')\n for i in range(len(S) -1 , -1 , -1):\n v = S[i]\n ... | 4 | 1 | ['Binary Search', 'Python', 'Python3'] | 0 |
shortest-distance-to-a-character | EASY SOLUTION | easy-solution-by-khushi_1502-i2vh | \n\n# Code\njava []\nclass Solution {\n public int[] shortestToChar(String s, char c) {\n List<Integer> l=new ArrayList<>();\n for(int i =0;i<s | khushi_1502 | NORMAL | 2024-11-22T10:46:37.193259+00:00 | 2024-11-22T10:46:37.193308+00:00 | 624 | false | \n\n# Code\n```java []\nclass Solution {\n public int[] shortestToChar(String s, char c) {\n List<Integer> l=new ArrayList<>();\n for(int i =0;i<s.length();i++)\n {\n if(s.charAt(i)==c)\n l.add(i);\n }\n int ans[]=new int[s.length()];\n for(int i=0;i<s.... | 3 | 0 | ['Java'] | 0 |
shortest-distance-to-a-character | SIMPLE TWO-POINTER C++ SOLUTION | simple-two-pointer-c-solution-by-jeffrin-x94y | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | Jeffrin2005 | NORMAL | 2024-07-20T12:43:10.678415+00:00 | 2024-07-20T12:43:41.271930+00:00 | 454 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:o(n+n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:o(n)\n<!-- Add your space complexity here, e.g. $... | 3 | 0 | ['C++'] | 0 |
shortest-distance-to-a-character | Easy Explanation || 3ms | easy-explanation-3ms-by-soumya2031-gfcu | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \nFirst collect all the i | soumya2031 | NORMAL | 2024-07-13T07:36:19.790934+00:00 | 2024-07-13T07:36:19.790955+00:00 | 568 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nFirst collect all the index of required character c in the string.\nThere are only 4 possibilities \n1) element == c\n2) element lies before the first appearance of c ... | 3 | 0 | ['Array', 'Java'] | 2 |
shortest-distance-to-a-character | Simple Java Code || Beats 100% | simple-java-code-beats-100-by-saurabh_mi-dsqj | Complexity\n- Time complexity:\nO(n)\n- Space complexity:\nO(n)\n# Code\n\nclass Solution {\n public int[] shortestToChar(String s, char c) {\n int n | Saurabh_Mishra06 | NORMAL | 2024-01-17T11:41:38.565187+00:00 | 2024-01-17T11:41:38.565209+00:00 | 508 | false | # Complexity\n- Time complexity:\nO(n)\n- Space complexity:\nO(n)\n# Code\n```\nclass Solution {\n public int[] shortestToChar(String s, char c) {\n int n = s.length();\n int[] output = new int[n];\n int cPosition = -n;\n\n for(int i=0; i<n; i++){\n if(s.charAt(i) == c){\n ... | 3 | 0 | ['Java'] | 0 |
shortest-distance-to-a-character | DarkNerd | Easy Solution | Iterative way | | darknerd-easy-solution-iterative-way-by-wg9by | Approach\n- Find out two index of character \'c\' which are side by side int the array. \n- Then find the sortest diffenece for all the characters between those | darknerd | NORMAL | 2024-01-12T06:23:16.736015+00:00 | 2024-01-12T06:23:16.736048+00:00 | 564 | false | # Approach\n- Find out two index of character \'c\' which are side by side int the array. \n- Then find the sortest diffenece for all the characters between those two index with the calculated two index.\n- For the characters in the starting not getting bound between two indexs of \'c\' using a dummy value `-10000`.\n-... | 3 | 0 | ['Array', 'Two Pointers', 'Java'] | 0 |
shortest-distance-to-a-character | VERY EASY TO UNDERSTAND | very-easy-to-understand-by-honeytechie-vzq0 | PLEASE UPVOTE IF YOU ANYWAY LIKE IT\n# Code\n\nclass Solution {\npublic:\n vector<int> shortestToChar(string s, char c) {\n vector<int>left(s.size(),- | honeytechie | NORMAL | 2023-06-02T20:00:50.013318+00:00 | 2023-06-02T20:00:50.013357+00:00 | 565 | false | # PLEASE UPVOTE IF YOU ANYWAY LIKE IT\n# Code\n```\nclass Solution {\npublic:\n vector<int> shortestToChar(string s, char c) {\n vector<int>left(s.size(),-1);\n int j=0;\n int flag=0;\n for(int i=0;i<s.size();i++)\n {\n if(s[i]==c && flag==1)\n {\n ... | 3 | 0 | ['C++'] | 0 |
shortest-distance-to-a-character | Solution in C++ | solution-in-c-by-ashish_madhup-5gg1 | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | ashish_madhup | NORMAL | 2023-04-15T17:34:45.053926+00:00 | 2023-04-15T17:34:45.053970+00:00 | 470 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 3 | 0 | ['C++'] | 0 |
shortest-distance-to-a-character | easy cpp solution | easy-cpp-solution-by-mohiteravi348-iiob | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | mohiteravi348 | NORMAL | 2023-01-27T05:12:06.992205+00:00 | 2023-01-27T05:12:06.992237+00:00 | 1,001 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 3 | 0 | ['C++'] | 0 |
shortest-distance-to-a-character | Java || Brute Force Solution | java-brute-force-solution-by-ritabrata_1-lq3r | \nclass Solution {\n public int[] shortestToChar(String s, char c) {\n List<Integer> list = new ArrayList<>();\n char c1[] = s.toCharArray();\n | Ritabrata_1080 | NORMAL | 2022-10-03T18:51:42.634048+00:00 | 2022-10-03T18:51:42.634101+00:00 | 1,129 | false | ```\nclass Solution {\n public int[] shortestToChar(String s, char c) {\n List<Integer> list = new ArrayList<>();\n char c1[] = s.toCharArray();\n for(int i = 0;i<c1.length;i++){\n if(c1[i] == c){\n list.add(i);\n }\n }\n int res[] = new int[c1.... | 3 | 0 | ['Java'] | 1 |
shortest-distance-to-a-character | Simple Javascript solution - faster than 80% only 1 loop | simple-javascript-solution-faster-than-8-b5co | You just push a 0 if the char is the c, and if not you calculate the min between the distance of the last 0 and the next c in the string using indexOf and lastI | dmarcosc | NORMAL | 2022-07-22T17:55:03.631198+00:00 | 2022-07-22T17:55:03.631248+00:00 | 355 | false | You just push a 0 if the char is the c, and if not you calculate the min between the distance of the last 0 and the next c in the string using indexOf and lastIndexOf\n\n````\n/**\n * @param {string} s\n * @param {character} c\n * @return {number[]}\n */\nvar shortestToChar = function(s, c) {\n \n let result = []... | 3 | 0 | ['JavaScript'] | 0 |
shortest-distance-to-a-character | Java || 98% faster / only one loop / simple / efficient / | java-98-faster-only-one-loop-simple-effi-dniz | \nclass Solution {\n public int[] shortestToChar(String s, char c) {\n int[] ans = new int[s.length()];\n \n for (int i = 0, j = 0; i < | csarmien318 | NORMAL | 2021-10-26T15:49:46.753273+00:00 | 2021-10-26T15:49:46.753315+00:00 | 603 | false | ```\nclass Solution {\n public int[] shortestToChar(String s, char c) {\n int[] ans = new int[s.length()];\n \n for (int i = 0, j = 0; i < ans.length; i++) {\n if (i == 0) j = s.indexOf(c);\n if (i > j && s.indexOf(c, i) >= 0 && Math.abs(j-i) > Math.abs(s.indexOf(c, i)-i)) ... | 3 | 0 | ['Java'] | 1 |
shortest-distance-to-a-character | C++ | O(n) | Easy to Understand | c-on-easy-to-understand-by-mantavya-7259 | \n\n\nclass Solution {\npublic:\n vector<int> shortestToChar(string s, char c) {\n \n int n=s.length();\n vector<int>ans(n,0);\n | mantavya | NORMAL | 2021-02-08T19:36:34.887093+00:00 | 2021-02-08T19:39:41.474457+00:00 | 124 | false | \n```\n\nclass Solution {\npublic:\n vector<int> shortestToChar(string s, char c) {\n \n int n=s.length();\n vector<int>ans(n,0);\n int lp=-999999;\n \n for(int i=0;i<n;i++)\n {\n if(s[i]==c)\n lp=i;\n \n ans[i]=i-lp;\n ... | 3 | 0 | [] | 0 |
shortest-distance-to-a-character | Any language | Two pass | O(n), 0ms | Very easy with detailed explanation | Example on Golang | any-language-two-pass-on-0ms-very-easy-w-hemo | How to find a minimal distance? We need to take into consideration only distance from closest letters from left and right side. No need to count distance from f | el777 | NORMAL | 2021-02-07T21:14:42.182883+00:00 | 2021-02-07T21:15:43.995611+00:00 | 157 | false | How to find a minimal distance? We need to take into consideration only distance from closest letters from left and right side. No need to count distance from far remote letters.\n\n\nNext, when we see a part of a string like here and suppose we want to calculate distance from `o` to closest `e`\'s.\n```\nleetcode\n ... | 3 | 0 | ['Go'] | 0 |
shortest-distance-to-a-character | Java Solution linear time | java-solution-linear-time-by-govindshaky-0ciy | ```\nclass Solution \n{\n public int[] shortestToChar(String s, char c) \n {\n int l = -1;\n int r = s.indexOf(c);\n int[] ans = new | govindshakya | NORMAL | 2021-02-07T14:52:09.640481+00:00 | 2021-02-07T14:52:09.640520+00:00 | 85 | false | ```\nclass Solution \n{\n public int[] shortestToChar(String s, char c) \n {\n int l = -1;\n int r = s.indexOf(c);\n int[] ans = new int[s.length()];\n \n for(int i = 0 ; i < s.length() ; i++)\n {\n if(s.charAt(i) == c )\n {\n l = r;\n... | 3 | 0 | [] | 0 |
shortest-distance-to-a-character | Declarative JavaScript Solution using Array.reduce() and Array.map() | declarative-javascript-solution-using-ar-6jkl | Modern declarative JavaScript use of string destructuring and array methods.\n\nPlease keep in mind, this is an example of declarative JavaScript programming vs | mrchakooo | NORMAL | 2020-09-30T17:40:48.311398+00:00 | 2020-09-30T17:40:48.311431+00:00 | 209 | false | Modern declarative JavaScript use of string destructuring and array methods.\n\nPlease keep in mind, this is an example of declarative JavaScript programming vs imperative solutions.\nThe solution won\'t be the fastest or the most memory efficient.\n\nHere\'s some references if you\'re more interested about what reduce... | 3 | 0 | ['JavaScript'] | 0 |
shortest-distance-to-a-character | javascript solution | javascript-solution-by-wvha-6cn7 | Probably not the best solution but it\'s straightforward and works \n\nSteps:\n- initialize an array \n- Loop through the string\n- find the closest character C | wvha | NORMAL | 2019-12-30T22:59:49.080827+00:00 | 2019-12-30T23:00:31.186690+00:00 | 376 | false | Probably not the best solution but it\'s straightforward and works \n\nSteps:\n- initialize an array \n- Loop through the string\n- find the closest character C with a helper function \n\n- at each letter, calculate and return the distance of the closest C \n- push that value distance to the array \n\n\n```\nvar shorte... | 3 | 0 | ['JavaScript'] | 1 |
shortest-distance-to-a-character | Python, with comments | python-with-comments-by-jcchoi-xg19 | \'\'\'\nclass Solution(object):\n def shortestToChar(self, S, C):\n\n res = []\n pos = [] \n \n # keep indexes of C in S\n | jcchoi | NORMAL | 2019-12-05T03:42:59.735038+00:00 | 2019-12-05T03:42:59.735074+00:00 | 287 | false | \'\'\'\nclass Solution(object):\n def shortestToChar(self, S, C):\n\n res = []\n pos = [] \n \n # keep indexes of C in S\n for i in range(len(S)):\n if S[i] == C:\n pos.append(i) \n \n for i in range(len(S)):\n # set... | 3 | 0 | ['Python'] | 0 |
shortest-distance-to-a-character | Python 3 O(1) auxiliary space one traversal algorithm | python-3-o1-auxiliary-space-one-traversa-cn86 | Instead of keeping O(2N) minimum distance list by traversing twice, I just kept the length of the substring that exists between the character C or edge and outp | ypark66 | NORMAL | 2019-10-22T19:19:34.789941+00:00 | 2019-10-22T19:24:23.114361+00:00 | 574 | false | Instead of keeping O(2N) minimum distance list by traversing twice, I just kept the length of the substring that exists between the character C or edge and output the distance.\n```\n def shortestToChar(self, S: str, C: str) -> List[int]:\n r, seen_c, l = [], False, 0\n for w in S:\n if w ==... | 3 | 0 | ['Python', 'Python3'] | 2 |
shortest-distance-to-a-character | python oneliner | python-oneliner-by-renzhang88-pdau | return [min(abs(i - ll) for ll in [i for i, e in enumerate(S) if e == C]) for i in range(len(S))] | renzhang88 | NORMAL | 2018-04-22T13:22:30.466868+00:00 | 2018-10-25T14:39:00.376769+00:00 | 776 | false | return [min(abs(i - ll) for ll in [i for i, e in enumerate(S) if e == C]) for i in range(len(S))] | 3 | 2 | [] | 3 |
shortest-distance-to-a-character | Check At Once and you dont have need to Check another solution..♥ | check-at-once-and-you-dont-have-need-to-pd6kq | IntuitionApproach
first i created arraylist in which i stored a index of that character to compare a minimum distance between indexes;
and then iterate a loop t | AbhiBhingole | NORMAL | 2025-04-02T13:26:01.687640+00:00 | 2025-04-02T13:26:01.687640+00:00 | 82 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
1) first i created arraylist in which i stored a index of that character to compare a minimum distance between indexes;
2) and then iterate a loop through string and check ... | 2 | 0 | ['Java'] | 0 |
shortest-distance-to-a-character | submission beat 100% of other submissions' runtime|| just simple traversing || TC->O(N)|| SC->O(N)|| | submission-beat-100-of-other-submissions-7adr | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | Rohan07_6473 | NORMAL | 2025-02-07T14:04:53.426682+00:00 | 2025-02-07T14:04:53.426682+00:00 | 235 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
`... | 2 | 0 | ['C++'] | 0 |
shortest-distance-to-a-character | Easy solution using arrays | 100% beats | easy-solution-using-arrays-100-beats-by-jdm1o | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | RAVINDRAN_S | NORMAL | 2024-11-21T05:05:56.256221+00:00 | 2024-11-21T05:05:56.256242+00:00 | 33 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:O(n^2)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(n)\n<!-- Add your space complexity here, e.g. $... | 2 | 0 | ['C++'] | 0 |
shortest-distance-to-a-character | 2 Pass Solution | O(n) time, O(1) auxiliary space | Python | 2-pass-solution-on-time-o1-auxiliary-spa-fp27 | Complexity\n- Time complexity: O(n)\n- Auxiliary Space complexity: O(1)\n- Overall Space complexity: O(n)\n\n# Code\n\nclass Solution:\n def shortestToChar(s | HariShankarKarthik | NORMAL | 2024-06-07T00:33:08.221000+00:00 | 2024-06-07T00:33:08.221027+00:00 | 171 | false | # Complexity\n- Time complexity: $$O(n)$$\n- Auxiliary Space complexity: $$O(1)$$\n- Overall Space complexity: $$O(n)$$\n\n# Code\n```\nclass Solution:\n def shortestToChar(self, s: str, c: str) -> List[int]:\n n = len(s)\n result = [float(\'inf\') for _ in range(n)]\n \n shortest_dist = ... | 2 | 0 | ['Array', 'Two Pointers', 'String', 'Python', 'Python3'] | 0 |
shortest-distance-to-a-character | simple and easy C++ solution 😍❤️🔥 | simple-and-easy-c-solution-by-shishirrsi-9c2r | if it\'s help, please up \u2B06 vote! \u2764\uFE0F\n\n\n# Code\n\nclass Solution {\npublic:\n vector<int> shortestToChar(string s, char c) \n {\n i | shishirRsiam | NORMAL | 2024-05-24T16:03:49.875657+00:00 | 2024-05-24T16:03:49.875697+00:00 | 524 | false | # if it\'s help, please up \u2B06 vote! \u2764\uFE0F\n\n\n# Code\n```\nclass Solution {\npublic:\n vector<int> shortestToChar(string s, char c) \n {\n int n = s.size();\n vector<int>ans, idx;\n for(int i=0;i<n;i++)\n if(s[i]==c) idx.push_back(i);\n \n int cur = 0, nex... | 2 | 0 | ['Array', 'Two Pointers', 'String', 'C++'] | 3 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.