Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

Java String Exercises: Rearrange a string so that all same characters become d distance away

Java String: Exercise-47 with Solution

Write a Java program to rearrange a string so that all same characters become d distance away.

Pictorial Presentation:

Java String Exercises: Rearrange a string so that all same characters become d distance away

Sample Solution:

Java Code:

import java.util.*;
public class Main {

 public static int MX = 26;
 static class freqOfChar {
  char c;
  int f;
  public freqOfChar(char c, int f) {
   this.c = c;
   this.f = f;
  }
 }

 public static void main(String[] args) {
  String strr = "accessories";
  System.out.println("The given string is: " + strr);
  System.out.println("The string after arrange newly is: ");
  String strg = charRearrange(strr, 4);
  if (strg != null)
   System.out.println(strg);
 }

 public static String charRearrange(String strg, int k) {
  if (strg.length() <= 1) return strg;

  freqOfChar[] chr_fre = new freqOfChar[MX];
  int ctr = 0;

  for (int i = 0; i < MX; i++) {
   chr_fre[i] = new freqOfChar((char)('a' + i), 0);
  }

  for (int i = 0; i < strg.length(); i++) {
   char ch = strg.charAt(i);
   chr_fre[ch - 'a'].f++;
   if (chr_fre[ch - 'a'].f == 1) ctr++;
  }

  bldOfHeap(chr_fre, MX);

  char[] str1 = new char[strg.length()];
  boolean[] occu = new boolean[strg.length()];
  for (int i = 0; i < ctr; i++) {
   freqOfChar chfreq = extractMax(chr_fre, MX - i);
   int ptr = i;
   while (occu[ptr]) ptr++;

   for (int j = 0; j < chfreq.f; j++) {
    if (ptr >= str1.length)
     return null;
    str1[ptr] = chfreq.c;
    occu[ptr] = true;
    ptr += k;
   }
  }
  return new String(str1);
 }

 private static void bldOfHeap(freqOfChar[] chr_fre, int size) {
  int i = (size - 1) / 2;
  while (i >= 0) {
   maxHeapify(chr_fre, i, size);
   i--;
  }
 }

 private static void swap(freqOfChar cf1, freqOfChar cf2) {
  char c = cf1.c;
  int f = cf1.f;
  cf1.c = cf2.c;
  cf1.f = cf2.f;
  cf2.c = c;
  cf2.f = f;
 }

 private static void maxHeapify(freqOfChar[] chr_fre, int node, int size) {
  int l = node * 2 + 1;
  int r = node * 2 + 2;
  int largest = node;
  if (l < size && chr_fre[l].f > chr_fre[node].f) {
   largest = l;
  }
  if (r < size && chr_fre[r].f > chr_fre[largest].f) {
   largest = r;
  }
  if (largest != node) {
   swap(chr_fre[node], chr_fre[largest]);
   maxHeapify(chr_fre, largest, size);
  }
 }
 private static freqOfChar extractMax(freqOfChar[] chr_fre, int size) {
  freqOfChar max = chr_fre[0];
  chr_fre[0] = chr_fre[size - 1];
  chr_fre[size - 1] = null;
  maxHeapify(chr_fre, 0, size - 1);
  return max;
 }
}

Sample Output:

The given string is: accessories
The string after arrange newly is: 
secrsecisao

Flowchart: 1

Flowchart: Java String Exercises - Rearrange a string so that all same characters become d distance away

Flowchart: 2

Flowchart: Java String Exercises - Rearrange a string so that all same characters become d distance away

Java Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a Java program to reverse every word in a string using methods.
Next: Write a Java program to remove "b" and "ac" from a given string.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Java: Tips of the Day

How to sort an ArrayList?

Collections.sort(testList);
Collections.reverse(testList);

That will do what you want. Remember to import Collections though!

Ref: https://bit.ly/32urdSe