what is the total distance of all characters (letters) from the beginning of a word to the end?
Given a string Southward and a character X where, for some. The task is to return an assortment of distances representing the shortest distance from the character X to every other character in the string.
Examples:
Input: S = "geeksforgeeks", X = 'due east'
Output: [1, 0, 0, i, ii, 3, 3, 2, 1, 0, 0, 1, two]
for S[0] = 'g' nearest 'e' is at distance = ane i.eastward. Due south[one] = 'east'.
similarly, for S[1] = 'east', distance = 0.
for Southward[six] = 'o', distance = iii since we have S[9] = 'due east', and so on.
Input: S = "helloworld", Ten = 'o'
Output: [four, 3, 2, one, 0, 1, 0, ane, 2, 3]
Approach one: For each grapheme at index i in S[], permit u.s. try to find the distance to the next graphic symbol X going left to right, and from right to left. The answer volition be the minimum of these ii values.
- When going from left to right, nosotros remember the index of the last graphic symbol 10 nosotros've seen. And then the answer is i – prev.
- When going from right to left, the answer is prev – i.
- We accept the minimum of these two answers to create our final distance assortment.
- Finally, impress the array.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using
namespace
std;
void
shortestDistance(string S,
char
X)
{
int
prev = INT_MAX;
vector<
int
> ans;
for
(
int
i = 0; i < S.length(); i++)
{
if
(S[i] == X)
prev = i;
if
(prev == INT_MAX)
ans.push_back(INT_MAX);
else
ans.push_back(i - prev);
}
prev = INT_MAX;
for
(
int
i = South.length() - 1; i >= 0; i--)
{
if
(S[i] == Ten)
prev = i;
if
(prev != INT_MAX)
ans[i] = min(ans[i], prev - i);
}
for
(
auto
val: ans)
cout << val <<
' '
;
}
int
main()
{
cord S =
"helloworld"
;
char
X =
'o'
;
shortestDistance(S, X);
return
0;
}
Java
import
java.util.*;
form
GFG
{
static
void
shortestDistance(Cord S,
char
10)
{
int
prev = Integer.MAX_VALUE;
Vector<Integer> ans =
new
Vector<>();
for
(
int
i =
0
; i < South.length(); i++)
{
if
(S.charAt(i) == X)
prev = i;
if
(prev == Integer.MAX_VALUE)
ans.add(Integer.MAX_VALUE);
else
ans.add(i - prev);
}
prev = Integer.MAX_VALUE;
for
(
int
i = S.length() -
1
; i >=
0
; i--)
{
if
(S.charAt(i) == 10)
prev = i;
if
(prev != Integer.MAX_VALUE)
ans.set(i, Math.min(ans.get(i), prev - i));
}
for
(Integer val: ans)
Organization.out.impress(val+
" "
);
}
public
static
void
main(String[] args)
{
Cord S =
"geeksforgeeks"
;
char
X =
'g'
;
shortestDistance(S, X);
}
}
Python3
def
shortestDistance(S, X):
inf
=
float
(
'inf'
)
prev
=
inf
ans
=
[]
for
i,j
in
enumerate
(S):
if
S[i]
=
=
X:
prev
=
i
if
(prev
=
=
inf) :
ans.append(inf)
else
:
ans.append(i
-
prev)
prev
=
inf
for
i
in
range
(
len
(S)
-
1
,
-
1
,
-
1
):
if
Due south[i]
=
=
X:
prev
=
i
if
(Ten !
=
inf):
ans[i]
=
min
(ans[i], prev
-
i)
return
ans
S
=
"geeksforgeeks"
X
=
"g"
impress
(shortestDistance(Southward, X))
C#
using
System;
using
Arrangement.Collections.Generic;
grade
GFG
{
public
static
void
shortestDistance(String S,
char
X){
int
prev =
int
.MaxValue;
Listing<
int
> ans =
new
Listing<
int
>();
for
(
int
i=0; i<S.Length; i++)
{
if
(South[i] == X)
prev = i;
if
(prev ==
int
.MaxValue)
ans.Add(
int
.MaxValue);
else
ans.Add(i-prev);
}
prev =
int
.MaxValue;
for
(
int
i=S.Length-i; i>=0; i--)
{
if
(S[i] == 10)
prev = i;
if
(prev !=
int
.MaxValue)
ans[i] = Math.Min(ans[i], prev-i);
}
foreach
(
var
i
in
ans)
Panel.Write(i +
" "
);
}
public
static
void
Main(Cord[] args)
{
String South =
"geeksforgeeks"
;
char
Ten =
'g'
;
shortestDistance(S, X);
}
}
Javascript
<script>
role
shortestDistance(South, X)
{
let prev = Number.MAX_VALUE;
allow ans = [];
for
(let i = 0; i < S.length; i++)
{
if
(S[i] == X)
prev = i;
if
(prev == Number.MAX_VALUE)
ans.push(Number.MAX_VALUE);
else
ans.button(i - prev);
}
prev = Number.MAX_VALUE;
for
(permit i = S.length - 1; i >= 0; i--)
{
if
(S[i] == Ten)
prev = i;
if
(prev != Number.MAX_VALUE)
ans[i] = Math.min(ans[i], prev - i);
}
for
(let val of ans)
document.write(val +
' '
);
}
let S =
"helloworld"
;
let X =
'o'
;
shortestDistance(S, X);
</script>
Output
4 3 ii 1 0 1 0 i 2 3
Approach 2: Create a list holding the occurrence of the graphic symbol and and then create ii pointers pointing two immediate locations in this list, now iterate over the string to observe the deviation from these 2 pointers and insert the minimum in the result listing. If pointer 2 is nearer to the current graphic symbol, motility the pointers one step alee.
- Create a list property positions of the required graphic symbol in the cord and an empty list to concord the effect array.
- Create two pointers to the list p1=0 and p2=0 if list length is i else p2=i
- Iterate over the string and compare the values at these pointers (v1=p1->value & v2=p2->value) with the current alphabetize(i).
- If i <= v1, then push v1-i in the result list.
- Else if i <= v2
- if i is nearer to v1, then push i-v1 in the effect list
- Else push v2-i in the result list and move pointer one pace forrard if possible
- Else push button i-v1 into the upshot list
- Render result list
Below is the implementation of the to a higher place approach:
C++
#include <bits/stdc++.h>
using
namespace
std;
vector<
int
> shortestToChar(string s,
char
c)
{
vector<
int
> list;
vector<
int
> res;
int
len = s.length();
for
(
int
i = 0; i < len; i++) {
if
(s[i] == c) {
list.push_back(i);
}
}
int
p1, p2, v1, v2;
int
fifty = list.size() - 1;
p1 = 0;
p2 = l > 0 ? 1 : 0;
for
(
int
i = 0; i < len; i++) {
v1 = list[p1];
v2 = listing[p2];
if
(i <= v1) {
res.push_back(v1 - i);
}
else
if
(i <= v2) {
if
(i - v1 < v2 - i) {
res.push_back(i - v1);
}
else
{
res.push_back(v2 - i);
p1 = p2;
p2 = p2 < l ? (p2 + 1) : p2;
}
}
else
{
res.push_back(i - v2);
}
}
return
res;
}
int
main()
{
string s =
"geeksforgeeks"
;
char
c =
'east'
;
vector<
int
> res = shortestToChar(south, c);
for
(
car
i : res)
cout << i <<
" "
;
return
0;
}
C
#include <stdio.h>
#define MAX_SIZE 100
void
shortestToChar(
char
due south[],
char
c,
int
* res)
{
int
list[MAX_SIZE];
int
len = 0;
int
l = 0;
while
(due south[len] !=
'\0'
) {
if
(due south[len] == c) {
list[50] = len;
l++;
}
len++;
}
int
p1, p2, v1, v2;
fifty = l - 1;
p1 = 0;
p2 = fifty > 0 ? 1 : 0;
for
(
int
i = 0; i < len; i++) {
v1 = list[p1];
v2 = listing[p2];
if
(i <= v1) {
res[i] = (v1 - i);
}
else
if
(i <= v2) {
if
(i - v1 < v2 - i) {
res[i] = (i - v1);
}
else
{
res[i] = (v2 - i);
p1 = p2;
p2 = p2 < 50 ? (p2 + 1) : p2;
}
}
else
{
res[i] = (i - v2);
}
}
}
int
primary()
{
char
s[] =
"geeksforgeeks"
;
char
c =
'e'
;
int
res[MAX_SIZE];
shortestToChar(due south, c, res);
int
i = 0;
while
(south[i] !=
'\0'
)
printf
(
"%d "
, res[i++]);
return
0;
}
Output
i 0 0 1 ii three 3 2 ane 0 0 ane two
Fourth dimension Complexity: O(n)
Space Complication: O(north)
rodrigueztherstre.blogspot.com
Source: https://www.geeksforgeeks.org/shortest-distance-to-every-other-character-from-given-character/
0 Response to "what is the total distance of all characters (letters) from the beginning of a word to the end?"
Post a Comment