Check if rearrange of characters can such that no two adjacent are same

Here given code implementation process.

// Java Program 
// Check if rearrange of characters can such that no two adjacent are same 
import java.util.HashMap;
public class Distinct
{

    public void rearrange(String text)
    {
        if (text.length() == 0)
        {
            return;
        }
        // Display given text
        System.out.print("\n Given text : " + text);
        // Use to collect character frequency
        HashMap < Character, Integer > record = new HashMap < Character, Integer > ();
        String result = "";
        int max = 0;
        // iterate the loop through by text length
        for (int i = 0; i < text.length(); ++i)
        {
            if (record.containsKey(text.charAt(i)))
            {
                // Increase frequency
                record.put(text.charAt(i), record.get(text.charAt(i)) + 1);
            }
            else
            {
                // Add character
                record.put(text.charAt(i), 1);
            }
            if (record.get(text.charAt(i)) > max)
            {
                // Get new higher element frequency
                max = record.get(text.charAt(i));
            }
        }
       
        if (max <= text.length() - max + 1)
        {
            System.out.print("\n Adjacent unique rearrange are possible \n");
        }
        else
        {
            System.out.print("\n Adjacent unique rearrange not possible \n");
        }
    }
    public static void main(String[] args)
    {
        Distinct task = new Distinct();
        // Test case
        task.rearrange("LLKKKXYKX");
        task.rearrange("12311111");
        task.rearrange("abc123ABC");
    }
}

Output

 Given text : LLKKKXYKX
 Adjacent unique rearrange are possible

 Given text : 12311111
 Adjacent unique rearrange not possible

 Given text : abc123ABC
 Adjacent unique rearrange are possible
// Include header file
#include <iostream>
#include <string>
#include <unordered_map>

using namespace std;

// C++ Program
// Check if rearrange of characters can such that no two adjacent are same

class Distinct
{
	public: void rearrange(string text)
	{
		if (text.length() == 0)
		{
			return;
		}
		// Display given text
		cout << "\n Given text : " << text;
		// Use to collect character frequency
		unordered_map < char, int > record ;
		string result = "";
		int max = 0;
		// iterate the loop through by text length
		for (int i = 0; i < text.length(); ++i)
		{
			if (record.find(text[i]) != record.end())
			{
				// Increase frequency
				record[text[i]] = record[text[i]] + 1;
			}
			else
			{
				// Add character
				record[text[i]] = 1;
			}
			if (record[text[i]] > max)
			{
				// Get new higher element frequency
				max = record[text[i]];
			}
		}
		if (max <= text.length() - max + 1)
		{
			cout << "\n Adjacent unique rearrange are possible \n";
		}
		else
		{
			cout << "\n Adjacent unique rearrange not possible \n";
		}
	}
};
int main()
{
	Distinct task = Distinct();
	// Test case
	task.rearrange("LLKKKXYKX");
	task.rearrange("12311111");
	task.rearrange("abc123ABC");
	return 0;
}

Output

 Given text : LLKKKXYKX
 Adjacent unique rearrange are possible

 Given text : 12311111
 Adjacent unique rearrange not possible

 Given text : abc123ABC
 Adjacent unique rearrange are possible
// Include namespace system
using System;
using System.Collections.Generic;

// C# Program
// Check if rearrange of characters can such that no two adjacent are same

public class Distinct
{
	public void rearrange(String text)
	{
		if (text.Length == 0)
		{
			return;
		}
		// Display given text
		Console.Write("\n Given text : " + text);
		// Use to collect character frequency
		Dictionary < char, int > record = new Dictionary < char, int > ();
		int max = 0;
		// iterate the loop through by text length
		for (int i = 0; i < text.Length; ++i)
		{
			if (record.ContainsKey(text[i]))
			{
				// Increase frequency
				record[text[i]] = record[text[i]] + 1;
			}
			else
			{
				// Add character
				record.Add(text[i], 1);
			}
			if (record[text[i]] > max)
			{
				// Get new higher element frequency
				max = record[text[i]];
			}
		}
		if (max <= text.Length - max + 1)
		{
			Console.Write("\n Adjacent unique rearrange are possible \n");
		}
		else
		{
			Console.Write("\n Adjacent unique rearrange not possible \n");
		}
	}
	public static void Main(String[] args)
	{
		Distinct task = new Distinct();
		// Test case
		task.rearrange("LLKKKXYKX");
		task.rearrange("12311111");
		task.rearrange("abc123ABC");
	}
}

Output

 Given text : LLKKKXYKX
 Adjacent unique rearrange are possible

 Given text : 12311111
 Adjacent unique rearrange not possible

 Given text : abc123ABC
 Adjacent unique rearrange are possible
<?php
// Php Program
// Check if rearrange of characters can such that no two adjacent are same
class Distinct
{
	public	function rearrange($text)
	{
		if (strlen($text) == 0)
		{
			return;
		}
		// Display given text
		echo "\n Given text : ". $text;
		// Use to collect character frequency
		$record = array();
		$max = 0;
		// iterate the loop through by text length
		for ($i = 0; $i < strlen($text); ++$i)
		{
			if (array_key_exists($text[$i], $record))
			{ // Increase frequency
				$record[$text[$i]] = $record[$text[$i]] + 1;
			}
			else
			{ // Add character
				$record[$text[$i]] = 1;
			}
			if ($record[$text[$i]] > $max)
			{
				// Get new higher element frequency
				$max = $record[$text[$i]];
			}
		}
		if ($max <= strlen($text) - $max + 1)
		{
			echo "\n Adjacent unique rearrange are possible \n";
		}
		else
		{
			echo "\n Adjacent unique rearrange not possible \n";
		}
	}
}

function main()
{
	$task = new Distinct();
	$task->rearrange("LLKKKXYKX");
	$task->rearrange("12311111");
	$task->rearrange("abc123ABC");
}
main();

Output

 Given text : LLKKKXYKX
 Adjacent unique rearrange are possible

 Given text : 12311111
 Adjacent unique rearrange not possible

 Given text : abc123ABC
 Adjacent unique rearrange are possible
// Node Js Program
// Check if rearrange of characters can such that no two adjacent are same
class Distinct
{
	rearrange(text)
	{
		if (text.length == 0)
		{
			return;
		}
		// Display given text
		process.stdout.write("\n Given text : " + text);
		// Use to collect character frequency
		var record = new Map();
		var max = 0;
		// iterate the loop through by text length
		for (var i = 0; i < text.length; ++i)
		{
			if (record.has(text.charAt(i)))
			{
				// Increase frequency
				record.set(text.charAt(i), record.get(text.charAt(i)) + 1);
			}
			else
			{
				// Add character
				record.set(text.charAt(i), 1);
			}
			if (record.get(text.charAt(i)) > max)
			{
				// Get new higher element frequency
				max = record.get(text.charAt(i));
			}
		}
		if (max <= text.length - max + 1)
		{
			process.stdout.write("\n Adjacent unique rearrange are possible \n");
		}
		else
		{
			process.stdout.write("\n Adjacent unique rearrange not possible \n");
		}
	}
}

function main()
{
	var task = new Distinct();
	// Test case
	task.rearrange("LLKKKXYKX");
	task.rearrange("12311111");
	task.rearrange("abc123ABC");
}
main();

Output

 Given text : LLKKKXYKX
 Adjacent unique rearrange are possible

 Given text : 12311111
 Adjacent unique rearrange not possible

 Given text : abc123ABC
 Adjacent unique rearrange are possible
#  Python 3 Program
#  Check if rearrange of characters can such that no two adjacent are same
class Distinct :
	def rearrange(self, text) :
		if (len(text) == 0) :
			return
		
		#  Display given text
		print("\n Given text : ", text, end = "")
		#  Use to collect character frequency
		record = dict()
		max = 0
		i = 0
		#  iterate the loop through by text length
		while (i < len(text)) :
			if (text[i] in record.keys()) :
				#  Increase frequency
				record[text[i]] = record.get(text[i]) + 1
			else :
				#  Add character
				record[text[i]] = 1
			
			if (record.get(text[i]) > max) :
				#  Get new higher element frequency
				max = record.get(text[i])
			
			i += 1
		
		if (max <= len(text) - max + 1) :
			print("\n Adjacent unique rearrange are possible ")
		else :
			print("\n Adjacent unique rearrange not possible ")
		
	

def main() :
	task = Distinct()
	#  Test case
	task.rearrange("LLKKKXYKX")
	task.rearrange("12311111")
	task.rearrange("abc123ABC")

if __name__ == "__main__": main()

Output

 Given text :  LLKKKXYKX
 Adjacent unique rearrange are possible

 Given text :  12311111
 Adjacent unique rearrange not possible

 Given text :  abc123ABC
 Adjacent unique rearrange are possible
#  Ruby Program
#  Check if rearrange of characters can such that no two adjacent are same
class Distinct 
	def rearrange(text) 
		if (text.length == 0) 
			return
		end

		#  Display given text
		print("\n Given text : ", text)
		#  Use to collect character frequency
		record = Hash.new 
		max = 0
		i = 0
		#  iterate the loop through by text length
		while (i < text.length) 
			if (record.key?(text[i])) 
				record[text[i]] = record[text[i]] + 1
			else 
				record[text[i]] = 1
			end

			if (record[text[i]] > max) 
				#  Get new higher element frequency
				max = record[text[i]]
			end

			i += 1
		end

		if (max <= text.length - max + 1) 
			print("\n Adjacent unique rearrange are possible \n")
		else 
			print("\n Adjacent unique rearrange not possible \n")
		end

	end

end

def main() 
	task = Distinct.new()
	#  Test case
	task.rearrange("LLKKKXYKX")
	task.rearrange("12311111")
	task.rearrange("abc123ABC")
end

main()

Output

 Given text : LLKKKXYKX
 Adjacent unique rearrange are possible 

 Given text : 12311111
 Adjacent unique rearrange not possible 

 Given text : abc123ABC
 Adjacent unique rearrange are possible 
import scala.collection.mutable._;
// Scala Program
// Check if rearrange of characters can such that no two adjacent are same
class Distinct
{
	def rearrange(text: String): Unit = {
		if (text.length() == 0)
		{
			return;
		}
		// Display given text
		print("\n Given text : " + text);
		// Use to collect character frequency
		var record = Map[Char, Int]();
		var max: Int = 0;
		var i: Int = 0;
		// iterate the loop through by text length
		while (i < text.length())
		{
			if (record.contains(text.charAt(i)))
			{
				// Increase frequency
				record.addOne(text.charAt(i), record.get(text.charAt(i)).get + 1);
			}
			else
			{
				// Add character
				record.addOne(text.charAt(i), 1);
			}
			if (record.get(text.charAt(i)).get > max)
			{
				// Get new higher element frequency
				max = record.get(text.charAt(i)).get;
			}
			i += 1;
		}
		if (max <= text.length() - max + 1)
		{
			print("\n Adjacent unique rearrange are possible \n");
		}
		else
		{
			print("\n Adjacent unique rearrange not possible \n");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Distinct = new Distinct();
		// Test case
		task.rearrange("LLKKKXYKX");
		task.rearrange("12311111");
		task.rearrange("abc123ABC");
	}
}

Output

 Given text : LLKKKXYKX
 Adjacent unique rearrange are possible

 Given text : 12311111
 Adjacent unique rearrange not possible

 Given text : abc123ABC
 Adjacent unique rearrange are possible
import Foundation
// Swift 4 Program
// Check if rearrange of characters can such that no two adjacent are same
class Distinct
{
	func rearrange(_ t: String)
	{
      	var text = Array(t);
		if (text.count == 0)
		{
			return;
		}
		// Display given text
		print("\n Given text : ", t, terminator: "");
		// Use to collect character frequency
		var record = [Character: Int]();
		var max: Int = 0;
		var i: Int = 0;
		// iterate the loop through by text length
		while (i < text.count)
		{
			if (record.keys.contains(text[i]))
			{
				// Increase frequency
				record[text[i]] = record[text[i]]! + 1;
			}
			else
			{
				// Add character
				record[text[i]] = 1;
			}
			if (record[text[i]]! > max)
			{
				// Get new higher element frequency
				max = record[text[i]]!;
			}
			i += 1;
		}
		if (max <= text.count - max + 1)
		{
			print("\n Adjacent unique rearrange are possible ");
		}
		else
		{
			print("\n Adjacent unique rearrange not possible ");
		}
	}
}
func main()
{
	let task: Distinct = Distinct();
	// Test case
	task.rearrange("LLKKKXYKX");
	task.rearrange("12311111");
	task.rearrange("abc123ABC");
}
main();

Output

 Given text :  LLKKKXYKX
 Adjacent unique rearrange are possible

 Given text :  12311111
 Adjacent unique rearrange not possible

 Given text :  abc123ABC
 Adjacent unique rearrange are possible
// Kotlin Program
// Check if rearrange of characters can such that no two adjacent are same
class Distinct
{
	fun rearrange(text: String): Unit
	{
		if (text.length == 0)
		{
			return;
		}
		// Display given text
		print("\n Given text : " + text);
		// Use to collect character frequency
		var record = mutableMapOf < Char , Int > (); 
		var max: Int = 0;
		var i: Int = 0;
		// iterate the loop through by text length
		while (i < text.length)
		{
			if (record.containsKey(text.get(i)))
			{
				// Increase frequency
				record.put(text.get(i), record.getValue(text.get(i)) + 1);
			}
			else
			{
				// Add character
				record.put(text.get(i), 1);
			}
			if (record.getValue(text.get(i)) > max)
			{
				// Get new higher element frequency
				max = record.getValue(text.get(i));
			}
			i += 1;
		}
		if (max <= text.length - max + 1)
		{
			print("\n Adjacent unique rearrange are possible \n");
		}
		else
		{
			print("\n Adjacent unique rearrange not possible \n");
		}
	}
}
fun main(args: Array < String > ): Unit
{
	var task: Distinct = Distinct();
	// Test case
	task.rearrange("LLKKKXYKX");
	task.rearrange("12311111");
	task.rearrange("abc123ABC");
}

Output

 Given text : LLKKKXYKX
 Adjacent unique rearrange are possible

 Given text : 12311111
 Adjacent unique rearrange not possible

 Given text : abc123ABC
 Adjacent unique rearrange are possible


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved