Given a string S and a character C, return an array of integers representing the shortest distance from the character C in the string. javascript
JavaScript
class Solution {
public:
vector<int> shortestToChar(string S, char C) {
vector<int> r(S.size(), 0);
int prev = -S.size();
for (int i = 0; i < S.size(); ++ i) {
if (S[i] == C) prev = i;
r[i] = i - prev;
}
prev = INT_MAX;
for (int i = S.size() - 1; i >= 0; -- i) {
if (S[i] == C) prev = i;
r[i] = min(r[i], prev - i);
}
return r;
}
};class Solution {
public:
vector<int> shortestToChar(string S, char C) {
int n = S.size();
vector<int> r(n, n);
for (int i = 0; i < n; ++ i) {
if (S[i] == C) r[i] = 0;
}
for (int i = 1; i < n; ++ i) {
r[i] = min(r[i], r[i - 1] + 1);
}
for (int i = n - 2; i >= 0; -- i) {
r[i] = min(r[i], r[i + 1] + 1);
}
return r;
}
};
Also in JavaScript:
- Title
- how to use a fixed time zone in nodejs
- Category
- JavaScript
- Title
- how to load localt ext file in js
- Category
- JavaScript
- Title
- how to output to console java
- Category
- JavaScript
- Title
- axios set authorization header
- Category
- JavaScript
- Title
- check if variable is undefined or null jquery
- Category
- JavaScript
- Title
- inner html jquery
- Category
- JavaScript
- Title
- how to freeze js object
- Category
- JavaScript
- Title
- Javascript compare two dates
- Category
- JavaScript
- Title
- how to run p5js in a particular section of page
- Category
- JavaScript
- Title
- convert array object to string javascript
- Category
- JavaScript
- Title
- clear terminal node js
- Category
- JavaScript
- Title
- how to find remainder in javascript
- Category
- JavaScript
- Title
- add webpack to react project
- Category
- JavaScript
- Title
- click doesn't work on ajax loaded data
- Category
- JavaScript
- Title
- add mute button to html5 video player
- Category
- JavaScript
- Title
- click point invert zoom react simple map
- Category
- JavaScript
- Title
- javascript after dom ready
- Category
- JavaScript
- Title
- javascript add nd st th to number
- Category
- JavaScript
- Title
- how to change input value in javascript using class
- Category
- JavaScript
- Title
- deprecationwarning: mongoose
- Category
- JavaScript
- Title
- javascript capitalize first letter
- Category
- JavaScript
- Title
- how to create request body javascript
- Category
- JavaScript
- Title
- angular interview questions
- Category
- JavaScript
- Title
- delete session javascript
- Category
- JavaScript
- Title
- compare the triplets hackerrank solution in javascript
- Category
- JavaScript
- Title
- How to get row index and cellindex together in javascript
- Category
- JavaScript
- Title
- convert object to array javascript
- Category
- JavaScript
- Title
- how to enable click copy function using js
- Category
- JavaScript
- Title
- hot get access_token instead of url
- Category
- JavaScript
- Title
- disabling submit button until all fields have values
- Category
- JavaScript
- Title
- function prototype javascript
- Category
- JavaScript
- Title
- how to create component in reactjs
- Category
- JavaScript
- Title
- get attribute value jquery
- Category
- JavaScript
- Title
- async iife
- Category
- JavaScript
- Title
- javascript array map stack
- Category
- JavaScript
- Title
- fetch api javascript
- Category
- JavaScript
- Title
- javascript check if value in array
- Category
- JavaScript
- Title
- importing project images in react native
- Category
- JavaScript
- Title
- iterate over array javascript
- Category
- JavaScript
- Title
- add 2 class names react
- Category
- JavaScript
- Title
- angular img
- Category
- JavaScript
- Title
- axios delete request payload
- Category
- JavaScript
- Title
- billie eilish
- Category
- JavaScript
- Title
- FUNCION EN OBJETO JAVASCRIPT
- Category
- JavaScript
- Title
- How do I check if an element is hidden in jQuery?
- Category
- JavaScript
- Title
- how to send get request to external server using node
- Category
- JavaScript
- Title
- javascript array column
- Category
- JavaScript
- Title
- boucle for of javascript
- Category
- JavaScript
- Title
- get all keys in json object
- Category
- JavaScript
- Title
- javascript change font color based on value
- Category
- JavaScript
- Title
- include jquery
- Category
- JavaScript
- Title
- how to remove element from array in javascript
- Category
- JavaScript
- Title
- ipv4 to int32 js
- Category
- JavaScript
- Title
- expo app.json
- Category
- JavaScript
- Title
- create react app in existing folder
- Category
- JavaScript
- Title
- javascript check if dom element exists
- Category
- JavaScript
- Title
- how to insert a value into an array javascript
- Category
- JavaScript
- Title
- javascript change url without reload
- Category
- JavaScript
- Title
- javascript check if elements of one array are in another
- Category
- JavaScript
- Title
- como ler um arquivo json com javascript
- Category
- JavaScript
- Title
- express validator always return error
- Category
- JavaScript
- Title
- how to know which button clicked in javascript
- Category
- JavaScript
- Title
- checked unchecked through js
- Category
- JavaScript
- Title
- embed jquery
- Category
- JavaScript
- Title
- How to include JSPs file from another folder
- Category
- JavaScript
- Title
- dropzone add download button
- Category
- JavaScript
- Title
- javascript base 10 to base 2
- Category
- JavaScript
- Title
- How can I know which radio button is selected via jQuery
- Category
- JavaScript
- Title
- how to get the timestamp in javascript
- Category
- JavaScript
- Title
- aws amplify get JWT TOKEN
- Category
- JavaScript
- Title
- javascript break out of loop
- Category
- JavaScript
- Title
- arrow function javascript ie11
- Category
- JavaScript
- Title
- angular build production
- Category
- JavaScript
- Title
- factorial javascript
- Category
- JavaScript
- Title
- append item to array javascript
- Category
- JavaScript
- Title
- how to validate an email address in javascript
- Category
- JavaScript
- Title
- how to set three js canvas width 100%
- Category
- JavaScript
- Title
- how to get element by title js
- Category
- JavaScript
- Title
- how to call javascript method using selectlist on change in vf page
- Category
- JavaScript
- Title
- how to create react app
- Category
- JavaScript
- Title
- how to call action from another module vuex
- Category
- JavaScript
- Title
- get window size javascript
- Category
- JavaScript
- Title
- for of array javascript
- Category
- JavaScript
- Title
- error sending empty object express
- Category
- JavaScript
- Title
- filter in react native
- Category
- JavaScript
- Title
- check value exist in array javascript
- Category
- JavaScript
- Title
- is typescript faster than javascript
- Category
- JavaScript
- Title
- how to check version of node js in cmd
- Category
- JavaScript
- Title
- Data Change with Mutation
- Category
- JavaScript
- Title
- get caret position javascript
- Category
- JavaScript
- Title
- d3 not reading json
- Category
- JavaScript
- Title
- foreach w3schools javascript
- Category
- JavaScript
- Title
- float to currency
- Category
- JavaScript
- Title
- getrect javascript
- Category
- JavaScript
- Title
- how to draw a flower in javascript
- Category
- JavaScript
- Title
- can't bookmark react router
- Category
- JavaScript
- Title
- convert string true to boolean true javascript
- Category
- JavaScript
- Title
- array javascript django
- Category
- JavaScript
- Title
- javascript change background color
- Category
- JavaScript
- Title
- document load javascript
- Category
- JavaScript