Skip to main content

Find out distance between similar characters in a string

Here given code implementation process.

// C program
// Find out distance between similar characters in a string

#include <stdio.h>
#include <string.h>

// Count all distance of similar character in given string
void charDistance(const char * text)
{
    // Get the size of given text
    int n = strlen(text);

    // Used to collecting distance of similar character
    int distance[255];
    // Use to count number of visiting character
    int visit[255];

    // Loop controlling variable
    int i   = 0;
    // Resultant variable
    int sum = 0;    

    // Set the initial default values
    // 255 indicates all possible alphabet character
    for (i = 0; i < 255; ++i)
    {
        visit[i] = 0;
        distance[i] = 0;
    }

    // Find distance of similar character
    for ( i = 0; i < n; ++i)
    {
        // Calculate distance
        sum += visit[text[i]] * i - distance[text[i]];
        // Add new distance
        distance[text[i]] += i; 
        // Increase char frequency
        visit[text[i]] ++;

    }
    printf("\n Text     : %s",text);
    // Display Calculated result
    printf("\n Distance : %d",sum);
}
int main(int argc, char const *argv[])
{
    const char * text = "MULTIMILLIONAIRE";

    charDistance(text);

    return 0;
}

Output

 Text     : MULTIMILLIONAIRE
 Distance : 47
/*
  Java program
  Find out distance between similar characters in a string
*/
public class Distance
{
	// Count all distance of similar character in given string
	public void charDistance(String text)
	{
		// Get the size of given text
		int n = text.length();
		// Used to collecting distance of similar character
		int[] distance = new int[255];
		// Use to count number of visiting character
		int[] visit = new int[255];
		// Loop controlling variable
		int i = 0;
		// Resultant variable
		int sum = 0;
		// Set the initial default values
		// 255 indicates all possible alphabet character
		for (i = 0; i < 255; ++i)
		{
			visit[i] = 0;
			distance[i] = 0;
		}
		// Find distance of similar character
		for (i = 0; i < n; ++i)
		{
			// Calculate distance
			sum += visit[text.charAt(i)] * i - distance[text.charAt(i)];
			// Add new distance
			distance[text.charAt(i)] += i;
			// Increase char frequency
			visit[text.charAt(i)]++;
		}
		System.out.print("\n Text     : " + text);
		// Display Calculated result
		System.out.print("\n Distance : " + sum);
	}
	public static void main(String[] args)
	{
		Distance task = new Distance();
		String text = "MULTIMILLIONAIRE";
		task.charDistance(text);
	}
}

Output

 Text     : MULTIMILLIONAIRE
 Distance : 47
// Include header file
#include <iostream>
#include <string>

using namespace std;
/*
  C++ program
  Find out distance between similar characters in a string
*/
class Distance
{
	public:
		// Count all distance of similar character in given string
		void charDistance(string text)
		{
			// Get the size of given text
			int n = text.length();
			// Used to collecting distance of similar character
			int distance[255];
			// Use to count number of visiting character
			int visit[255];
			// Loop controlling variable
			int i = 0;
			// Resultant variable
			int sum = 0;
			// Set the initial default values
			// 255 indicates all possible alphabet character
			for (i = 0; i < 255; ++i)
			{
				visit[i] = 0;
				distance[i] = 0;
			}
			// Find distance of similar character
			for (i = 0; i < n; ++i)
			{
				// Calculate distance
				sum += visit[text[i]] *i - distance[text[i]];
				// Add new distance
				distance[text[i]] += i;
				// Increase char frequency
				visit[text[i]]++;
			}
			cout << "\n Text     : " << text;
			// Display Calculated result
			cout << "\n Distance : " << sum;
		}
};
int main()
{
	Distance task = Distance();
	string text = "MULTIMILLIONAIRE";
	task.charDistance(text);
	return 0;
}

Output

 Text     : MULTIMILLIONAIRE
 Distance : 47
// Include namespace system
using System;
/*
  C# program
  Find out distance between similar characters in a string
*/
public class Distance
{
	// Count all distance of similar character in given string
	public void charDistance(String text)
	{
		// Get the size of given text
		int n = text.Length;
		// Used to collecting distance of similar character
		int[] distance = new int[255];
		// Use to count number of visiting character
		int[] visit = new int[255];
		// Loop controlling variable
		int i = 0;
		// Resultant variable
		int sum = 0;
		// Find distance of similar character
		while (i < n)
		{
			// Calculate distance
			sum += visit[text[i]] * i - distance[text[i]];
			// Add new distance
			distance[text[i]] += i;
			// Increase char frequency
			visit[text[i]]++;
			++i;
		}
		Console.Write("\n Text     : " + text);
		// Display Calculated result
		Console.Write("\n Distance : " + sum);
	}
	public static void Main(String[] args)
	{
		Distance task = new Distance();
		String text = "MULTIMILLIONAIRE";
		task.charDistance(text);
	}
}

Output

 Text     : MULTIMILLIONAIRE
 Distance : 47
<?php
/*
  Php program
  Find out distance between similar characters in a string
*/
class Distance
{
	// Count all distance of similar character in given string
	public	function charDistance($text)
	{
		// Get the size of given text
		$n = strlen($text);
		// Used to collecting distance of similar character
		$distance = array_fill(0, 255, 0);
		// Use to count number of visiting character
		$visit = array_fill(0, 255, 0);
		// Loop controlling variable
		$i = 0;
		// Resultant variable
		$sum = 0;
		// Find distance of similar character
		for ($i = 0; $i < $n; ++$i)
		{
			// Calculate distance
			$sum += $visit[ord($text[$i])] * $i - $distance[ord($text[$i])];
			// Add new distance
			$distance[ord($text[$i])] += $i;
			// Increase char frequency
			$visit[ord($text[$i])]++;
		}
		echo "\n Text     : ". $text;
		// Display Calculated result
		echo "\n Distance : ". $sum;
	}
}

function main()
{
	$task = new Distance();
	$text = "MULTIMILLIONAIRE";
	$task->charDistance($text);
}
main();

Output

 Text     : MULTIMILLIONAIRE
 Distance : 47
/*
  Node Js program
  Find out distance between similar characters in a string
*/
class Distance
{
	// Count all distance of similar character in given string
	charDistance(text)
	{
		// Get the size of given text
		var n = text.length;
		// Used to collecting distance of similar character
		var distance = Array(255).fill(0);
		// Use to count number of visiting character
		var visit = Array(255).fill(0);
		// Loop controlling variable
		var i = 0;
		// Resultant variable
		var sum = 0;
		// Find distance of similar character
		for (i = 0; i < n; ++i)
		{
			// Calculate distance
			sum += visit[text.charAt(i).charCodeAt(0)] * i - distance[text.charAt(i).charCodeAt(0)];
			// Add new distance
			distance[text.charAt(i).charCodeAt(0)] += i;
			// Increase char frequency
			visit[text.charAt(i).charCodeAt(0)]++;
		}
		process.stdout.write("\n Text     : " + text);
		// Display Calculated result
		process.stdout.write("\n Distance : " + sum);
	}
}

function main()
{
	var task = new Distance();
	var text = "MULTIMILLIONAIRE";
	task.charDistance(text);
}
main();

Output

 Text     : MULTIMILLIONAIRE
 Distance : 47
#   Python 3 program
#   Find out distance between similar characters in a string

class Distance :
	#  Count all distance of similar character in given string
	def charDistance(self, text) :
		#  Get the size of given text
		n = len(text)
		#  Used to collecting distance of similar character
		distance = [0] * (255)
		#  Use to count number of visiting character
		visit = [0] * (255)
		#  Loop controlling variable
		i = 0
		#  Resultant variable
		sum = 0
		#  Find distance of similar character
		i = 0
		while (i < n) :
			#  Calculate distance
			sum += visit[ord(text[i])] * i - distance[ord(text[i])]
			#  Add new distance
			distance[ord(text[i])] += i
			#  Increase char frequency
			visit[ord(text[i])] += 1
			i += 1
		
		print("\n Text     : ", text, end = "")
		#  Display Calculated result
		print("\n Distance : ", sum, end = "")
	

def main() :
	task = Distance()
	text = "MULTIMILLIONAIRE"
	task.charDistance(text)

if __name__ == "__main__": main()

Output

 Text     :  MULTIMILLIONAIRE
 Distance :  47
#   Ruby program
#   Find out distance between similar characters in a string

class Distance 
	#  Count all distance of similar character in given string
	def charDistance(text) 
		#  Get the size of given text
		n = text.length
		#  Used to collecting distance of similar character
		distance = Array.new(255) {0}
		#  Use to count number of visiting character
		visit = Array.new(255) {0}
		#  Loop controlling variable
		i = 0
		#  Resultant variable
		sum = 0
		#  Find distance of similar character
		while (i < n) 
			#  Calculate distance
			sum += visit[text[i].ord] * i - distance[text[i].ord]
			#  Add new distance
			distance[text[i].ord] += i
			#  Increase char frequency
			visit[text[i].ord] += 1
			i += 1
		end

		print("\n Text     : ", text)
		#  Display Calculated result
		print("\n Distance : ", sum)
	end

end

def main() 
	task = Distance.new()
	text = "MULTIMILLIONAIRE"
	task.charDistance(text)
end

main()

Output

 Text     : MULTIMILLIONAIRE
 Distance : 47
import scala.collection.mutable._;
/*
  Scala program
  Find out distance between similar characters in a string
*/
class Distance
{
	// Count all distance of similar character in given string
	def charDistance(text: String): Unit = {
		// Get the size of given text
		var n: Int = text.length();
		// Used to collecting distance of similar character
		var distance: Array[Int] = Array.fill[Int](255)(0);
		// Use to count number of visiting character
		var visit: Array[Int] = Array.fill[Int](255)(0);
		// Loop controlling variable
		var i: Int = 0;
		// Resultant variable
		var sum: Int = 0;
		// Find distance of similar character
		while (i < n)
		{
			// Calculate distance
			sum += visit(text.charAt(i)) * i - distance(text.charAt(i));
			// Add new distance
			distance(text.charAt(i)) += i;
			// Increase char frequency
			visit(text.charAt(i)) += 1;
			i += 1;
		}
		print("\n Text     : " + text);
		// Display Calculated result
		print("\n Distance : " + sum);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Distance = new Distance();
		var text: String = "MULTIMILLIONAIRE";
		task.charDistance(text);
	}
}

Output

 Text     : MULTIMILLIONAIRE
 Distance : 47
/*
  Swift 4 program
  Find out distance between similar characters in a string
*/
class Distance
{
	// Count all distance of similar character in given string
	func charDistance(_ data: String)
	{
		// Get the size of given text
		let n: Int = data.count;
      	let text = Array(data);
		// Used to collecting distance of similar character
		var distance: [Int] = Array(repeating: 0, count: 255);
		// Use to count number of visiting character
		var visit: [Int] = Array(repeating: 0, count: 255);
		// Loop controlling variable
		var i: Int = 0;
		// Resultant variable
		var sum: Int = 0;
      	var index = 0;
		// Find distance of similar character
		while (i < n)
		{
           index =  Int(UnicodeScalar(String(text[i]))!.value);
			// Calculate distance
			sum += visit[index] * i - distance[index];
			// Add new distance
			distance[index] += i;
			// Increase char frequency
			visit[index] += 1;
			i += 1;
		}
		print("\n Text     : ", data, terminator: "");
		// Display Calculated result
		print("\n Distance : ", sum, terminator: "");
	}
}
func main()
{
	let task: Distance = Distance();
	let text: String = "MULTIMILLIONAIRE";
	task.charDistance(text);
}
main();

Output

 Text     :  MULTIMILLIONAIRE
 Distance :  47
/*
  Kotlin program
  Find out distance between similar characters in a string
*/
class Distance
{
	// Count all distance of similar character in given string
	fun charDistance(text: String): Unit
	{
		// Get the size of given text
		var n: Int = text.length;
		// Used to collecting distance of similar character
		var distance: Array < Int > = Array(255)
		{
			0
		};
		// Use to count number of visiting character
		var visit: Array < Int > = Array(255)
		{
			0
		};
		// Loop controlling variable
		var i: Int = 0;
		// Resultant variable
		var sum: Int = 0;
		// Find distance of similar character
		while (i < n)
		{
			// Calculate distance
			sum += visit[text.get(i).toInt()] * i - distance[text.get(i).toInt()];
			// Add new distance
			distance[text.get(i).toInt()] += i;
			// Increase char frequency
			visit[text.get(i).toInt()] += 1;
			i += 1;
		}
		print("\n Text     : " + text);
		// Display Calculated result
		print("\n Distance : " + sum);
	}
}
fun main(args: Array < String > ): Unit
{
	var task: Distance = Distance();
	var text: String = "MULTIMILLIONAIRE";
	task.charDistance(text);
}

Output

 Text     : MULTIMILLIONAIRE
 Distance : 47




Comment

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