Minimize the String by finding the shortest path from one end to another
Last Updated :
27 Jun, 2023
Given a string str representing a direct connection between arr[i] and arr[i+1] in the given string, the task is to minimize the string by finding the shortest path from one end to another end.
Examples:
Input: str = “ABDCEDZ”
Output: ABDZ
Explanation: Here our starting point is A and our ending point is Z So the shortest path is A->B->D->Z. In input, the movement is A->B->D->C->E->D->Z but we can move directly from D->Z, no need to move from D->C->E->D->Z so the final output is A->B->D->Z.
Input: str = “ABC”
Output: ABC
Explanation: here is the single path to move from A to C . A -> B -> C
Input: str = “ADBDE”
Output: ADE
Explanation: Starting point is A and the Ending point is E, Here we can see D connected with W so no need to move A->D->B->D->E just move A->D->E.
Approach: Follow the approach below to understand the implementation:
In this approach we use unordered_map to store the position of the character in output ‘ans’ string, whenever we get the same character we minimize our string ‘ans’.
Follow the steps to solve the problem:
- Store the destination in a variable.
- Take an unordered_map to store every character and its indexes.
- If the current character is already present in the map means we have come to the same point again so erase our current answer string from that index to the last point.
- When we get our destination character stop there and return the answer string.
Below is the code for the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string minimizeString(string str)
{
int n = str.length();
int last = str[n - 1];
string ans = "" ;
unordered_map< char , int > mp;
for ( int i = 0; i < n; i++) {
if (str[i] == last) {
ans += str[i];
return ans;
}
if (mp.find(str[i]) == mp.end()) {
ans += str[i];
mp[str[i]] = i;
}
else {
int pos = mp[str[i]];
ans = ans.substr(0, pos + 1);
}
}
}
int main()
{
string str = "ABDCEDZ" ;
cout << minimizeString(str);
return 0;
}
|
Java
import java.util.*;
class GFG {
public static String minimizeString(String str)
{
int n = str.length();
int last = str.charAt(n - 1 );
StringBuilder ans = new StringBuilder();
Map<Character, Integer> mp = new HashMap<>();
for ( int i = 0 ; i < n; i++) {
if (str.charAt(i) == last) {
ans.append(str.charAt(i));
return ans.toString();
}
if (!mp.containsKey(str.charAt(i))) {
ans.append(str.charAt(i));
mp.put(str.charAt(i), i);
}
else {
int pos = mp.get(str.charAt(i));
ans.delete(pos + 1 , ans.length());
}
}
return ans.toString();
}
public static void main(String[] args)
{
String str = "ABDCEDZ" ;
System.out.println(minimizeString(str));
}
}
|
Python3
def minimizeString(s):
n = len (s)
last = s[n - 1 ]
ans = ""
mp = {}
for i in range (n):
if s[i] = = last:
ans + = s[i]
return ans
if s[i] not in mp:
ans + = s[i]
mp[s[i]] = i
else :
pos = mp[s[i]]
ans = ans[:pos + 1 ]
return ans
str = "ABDCEDZ"
print (minimizeString( str ))
|
C#
using System;
using System.Collections.Generic;
class Program {
static string MinimizeString( string str)
{
int n = str.Length;
int last = str[n - 1];
string ans = "" ;
Dictionary< char , int > mp
= new Dictionary< char , int >();
for ( int i = 0; i < n; i++) {
if (str[i] == last) {
ans += str[i];
return ans;
}
if (!mp.ContainsKey(str[i])) {
ans += str[i];
mp.Add(str[i], i);
}
else {
int pos = mp[str[i]];
ans = ans.Substring(0, pos + 1);
}
}
return ans;
}
static void Main( string [] args)
{
string str = "ABDCEDZ" ;
Console.WriteLine(MinimizeString(str));
}
}
|
Javascript
function minimizeString(str)
{
const n = str.length;
const last = str[n - 1];
let ans = "" ;
const mp = new Map();
for (let i = 0; i < n; i++) {
if (str[i] === last) {
ans += str[i];
return ans;
}
if (!mp.has(str[i])) {
ans += str[i];
mp.set(str[i], i);
}
else {
const pos = mp.get(str[i]);
ans = ans.substr(0, pos + 1);
}
}
return ans;
}
const str = "ABDCEDZ" ;
console.log(minimizeString(str));
|
Time complexity: O(n2)
Auxiliary Space: O(n)
Share your thoughts in the comments
Please Login to comment...