Improve Article

Save Article

Like Article

Improve Article

Save Article

Like Article

Given a drawstring **S** and an integer **K**. You Can take immoderate characteristic from S and switch each occurrences of that characteristic in S pinch either 0 aliases 1, up to K times, The task is to find whether it is imaginable to create an alternating binary drawstring from S aft performing these replacements. If it is imaginable to create an alternating binary string, output The series of replacements made. Otherwise, output “NO”.

**Note: **An alternating binary drawstring is simply a drawstring consisting only of 0s and 1s specified that nary 2 adjacent characters are The same.

**Examples:**

**Input: **S = “akmzaxazmk”, K =** **6**Output: **YES, {a : 0, k : 1, m : 0, z : 1, x : 1}**Explanation:**

**First operation:**Replace each occurrences of ‘a’ pinch 0 then, S =**0**kmz**0**x**0**zmk**Second operation:**Replace each occurrences of ‘k’ pinch 1 then, S = 0**1**mz0x0zm**1****Third operation:**Replace each occurrences of ‘m’ pinch 0 then, S = 01**0**z0x0z**0**1**Fourth operation:**Replace each occurrences of ‘z’ pinch 1 then, S = 010**1**0x0**1**01**Fifth operation:**Replace each occurrences of ‘x’ pinch 1 then, S = 01010**1**0101

After 5 (Which are <=6) operations The drawstring is converted into alternating binary string.

**Input: **S = “axnyyjk”, K= 1**Output: **NO**Explanation: **It Can beryllium verified that The S can’t beryllium converted into alternating binary drawstring utilizing cognition astatine astir K times.

**Approach: **To lick The problem travel The beneath idea:

The problem Can beryllium solved utilizing HashMap information structure. Let america disagreement The problem into 3 parts and cognize reply of each.

- Checking for validity, Whether conversion in alternating binary drawstring is imaginable aliases not?
- Is it imaginable nether astatine astir K operations?
- Which characteristic should beryllium switch by 1 aliases 0?

**Validity:**It must beryllium noted that if each occurrences of a characteristic occurs either astatine overseas aliases moreover indices, Then that characteristic is valid for replacing pinch either 0 aliases 1. This Can beryllium checked by calculating The quality betwixt The indices of adjacent occurrences of aforesaid character. Formally, If 2 characters are same, Then The**difference betwixt their indices must beryllium even**(0 based indexing).- For example: S = “axada”. characteristic ‘a’ is placed astatine scale 0, 2, and 4 respectively. The quality betwixt adjacent occurrence is even. Formally, (2-0) and (4-2) is even.

**Number of operations required:**As it is fixed that, We Can usage cognition astatine astir K times, Which intends we Can switch each occurrences of astatine astir K chopped characters’ to either into 0 aliases 1. This Can beryllium cheque by size of The HashMap. If HashMap contains little than aliases adjacent to K chopped characters, Then conversion into alternating binary drawstring is imaginable other not.**Required characteristic for replacement:**Just traverse HashMap and switch The characteristic pinch 0 if scale corresponding to Character is moreover other switch The characteristic pinch 1(Vice – versa is besides possible).

Below are The steps for The supra approach:

- Initialize a boolean adaptable
**flag**and people it initially true. - Declare a HashMap let’s opportunity
**map**for storing indices. - Traverse connected The string,
- For each characteristic in The input string, cheque if it has appeared earlier in The HashMap, and cheque if The quality betwixt The indices of The existent occurrence and The erstwhile occurrence is even. If The quality is even, proceed pinch The adjacent iteration, otherwise, group The emblem to
**false**. - If The existent characteristic is not coming in The representation past adhd it to The HashMap pinch its scale value.
- Else people The emblem arsenic mendacious and break The loop.

- For each characteristic in The input string, cheque if it has appeared earlier in The HashMap, and cheque if The quality betwixt The indices of The existent occurrence and The erstwhile occurrence is even. If The quality is even, proceed pinch The adjacent iteration, otherwise, group The emblem to
- If The emblem is existent and map.size() is little than aliases adjacent to K, people YES other people NO.
- Traverse The map, if The characteristic is astatine an moreover scale past people 0 other 1 for The corresponding character.

Below is The codification for The supra approach:

## Java

import java.util.*;

public people GFG {

public fixed void main(String[] args)

{

String str = "akmzaxazmk";

int K = 6;

AlterString(str, K);

}

static void AlterString(String str, int K)

{

boolean emblem = true;

HashMap<Character, Integer> representation = caller HashMap<>();

for (int one = 0; one < str.length(); i++) {

if (map.containsKey(str.charAt(i))) {

int extremity = map.get(str.charAt(i));

if ((end - i) % 2 == 0) {

continue;

}

else {

flag = false;

break;

}

}

else {

map.put(str.charAt(i), i);

}

}

if (flag && map.size() <= K) {

System.out.println("YES");

for (Map.Entry<Character, Integer> group :

map.entrySet()) {

System.out.println(

" " + set.getKey() + " : "

+ ((set.getValue() % 2 == 0) ? 0 : 1));

}

}

else {

System.out.println("NO");

}

}

}

**Output**

**Time Complexity:** O(N)**Auxiliary Space: **O(26) = **~**O(1), As HashMap is utilized for storing indices. There Can beryllium astatine astir 26 different alphabets in S.

Like Article

Save Article

**Editor:**Naga

**Beritaja.com**at