Minimum Moves for adjacent stars
Last Updated :
31 Dec, 2023
Given a string s consisting of ‘.’ and ‘*’, In one move you can shift ‘*’, one index left or one index right if there is a ‘.’ present there, the task is to make an arrangement such that all the * are adjacent to each other i.e. there is no ‘.’ between any two ‘*’ Print the minimum number of moves required to do so.
Examples:
Input: s = **.*..
Output: 1
Explanation: Shift the * at index 3 to index 2
Input: s = *.*…*.**
Output: 9
Approach: To solve the problem follow the below idea:
The idea is to first identify the position of the middle ‘*’ character, then calculate the absolute difference between each ‘*’ character and its expected position in the rearranged sequence, adding up these differences to obtain the total minimum moves.
Step-by-step approach:
- Count the number of ‘*’ characters in the count
- Find the middle ‘*’ position in pos:
- Calculate the minimum moves:
- Initialize curr_count on the position of the middle ‘*’ character by corr_count = pos – count / 2.
- Iterate through the string for i = 0 to n:
- For each ‘*’ character encountered, calculate the absolute difference between the current position and the position of the ‘*’ character.
- Add this absolute difference to the answer ans (i.e, ans += abs(corr_count – i))
- Increment the current position.
- Return the ans
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
long long solve( int n, string& s)
{
int count = 0;
for ( auto x : s)
count += (x == '*' ? 1 : 0);
int pos = -1;
int corr_count = -1;
for ( int i = 0; i < n; i++) {
if (s[i] == '*' ) {
corr_count++;
if (corr_count == count / 2)
pos = i;
}
}
long long ans = 0;
corr_count = pos - count / 2;
for ( int i = 0; i < n; i++)
if (s[i] == '*' ) {
ans += abs (corr_count - i);
corr_count++;
}
return ans;
}
int main()
{
long long n = 10;
string s = "*.*...*.**";
long long result = solve(n, s);
cout << result << endl;
return 0;
}
|
Java
import java.util.Scanner;
public class MinMoves {
static long solve( int n, String s)
{
int count = 0 ;
for ( char x : s.toCharArray())
count += (x == '*' ? 1 : 0 );
int pos = - 1 ;
int corr_count = - 1 ;
for ( int i = 0 ; i < n; i++) {
if (s.charAt(i) == '*' ) {
corr_count++;
if (corr_count == count / 2 )
pos = i;
}
}
long ans = 0 ;
corr_count = pos - count / 2 ;
for ( int i = 0 ; i < n; i++)
if (s.charAt(i) == '*' ) {
ans += Math.abs(corr_count - i);
corr_count++;
}
return ans;
}
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
int n = 10 ;
String s = "*.*...*.**" ;
long result = solve(n, s);
System.out.println(result);
scanner.close();
}
}
|
Python3
def solve(n, s):
count = s.count( '*' )
pos = - 1
corr_count = - 1
for i in range (n):
if s[i] = = '*' :
corr_count + = 1
if corr_count = = count / / 2 :
pos = i
ans = 0
corr_count = pos - count / / 2
for i in range (n):
if s[i] = = '*' :
ans + = abs (corr_count - i)
corr_count + = 1
return ans
n = 10
s = "*.*...*.**"
result = solve(n, s)
print (result)
|
C#
using System;
class Program
{
static long Solve( int n, string s)
{
int count = 0;
foreach ( char x in s)
{
count += (x == '*' ? 1 : 0);
}
int pos = -1;
int corrCount = -1;
for ( int i = 0; i < n; i++)
{
if (s[i] == '*' )
{
corrCount++;
if (corrCount == count / 2)
{
pos = i;
}
}
}
long ans = 0;
corrCount = pos - count / 2;
for ( int i = 0; i < n; i++)
{
if (s[i] == '*' )
{
ans += Math.Abs(corrCount - i);
corrCount++;
}
}
return ans;
}
static void Main( string [] args)
{
int n = 10;
string s = "*.*...*.**" ;
long result = Solve(n, s);
Console.WriteLine(result);
Console.ReadLine();
}
}
|
Javascript
function solve(n, s) {
const count = (s.match(/\*/g) || []).length;
let pos = -1;
let corrCount = -1;
for (let i = 0; i < n; i++) {
if (s[i] === '*' ) {
corrCount += 1;
if (corrCount === Math.floor(count / 2)) {
pos = i;
}
}
}
let ans = 0;
corrCount = pos - Math.floor(count / 2);
for (let i = 0; i < n; i++) {
if (s[i] === '*' ) {
ans += Math.abs(corrCount - i);
corrCount += 1;
}
}
return ans;
}
const n = 10;
const s = "*.*...*.**" ;
const result = solve(n, s);
console.log(result);
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...