Check if permutation of a number is same to other number

Here given code implementation process.

import java.util.Arrays;
/*
    Java program for
    Check if permutation of a number is same to other number
*/
public class Permutation
{
	public int absValue(int x)
	{
		if (x < 0)
		{
			return -x;
		}
		return x;
	}
	public void isEquivalent(int a, int b)
	{
		boolean result = true;
		// Make valid positive number
		int t1 = absValue(a);
		int t2 = absValue(b);
		// Convert number to character array 
		// Because It's suitable for sorting
		char[] s1 = ("" + t1).toCharArray();
		char[] s2 = ("" + t2).toCharArray();
		if (s1.length == s2.length)
		{
			// Sort both array
			Arrays.sort(s1);
			Arrays.sort(s2);
			// Convert character array to string
			String v1 = new String(s1);
			String v2 = new String(s2);
			if (!v1.equals(v2))
			{
				// When both string is not equal
				result = false;
			}
		}
		else
		{
			result = false;
		}
		// Display given number
		System.out.print("\n Given number a : " + a);
		System.out.print("\n Given number b : " + b);
		if (result)
		{
			System.out.print("\n Result : Yes \n");
		}
		else
		{
			System.out.print("\n Result : No \n");
		}
	}
	public static void main(String[] args)
	{
		Permutation task = new Permutation();
		// Test
		task.isEquivalent(123, 312);
		task.isEquivalent(12, 122);
		task.isEquivalent(1200, 1002);
		task.isEquivalent(1223, 3125);
	}
}

Output

 Given number a : 123
 Given number b : 312
 Result : Yes

 Given number a : 12
 Given number b : 122
 Result : No

 Given number a : 1200
 Given number b : 1002
 Result : Yes

 Given number a : 1223
 Given number b : 3125
 Result : No
// Include header file
#include <iostream>
#include <string>
#include <algorithm>

using namespace std;
/*
    C++ program for
    Check if permutation of a number is same to other number
*/
class Permutation
{
	public: int absValue(int x)
	{
		if (x < 0)
		{
			return -x;
		}
		return x;
	}
	void isEquivalent(int a, int b)
	{
		bool result = true;
		// Make valid positive number
		int t1 = this->absValue(a);
		int t2 = this->absValue(b);
		// Convert number to character array 
		// Because It's suitable for sorting
		string s1 = to_string(t1);
		string s2 = to_string(t2);
		if (s1.length()==s2.length())
		{
			// Sort both array
			sort(s1.begin(), s1.end());
			sort(s2.begin(), s2.end());
	
			if (s1.compare(s2) != 0)
			{
				// When both string is not equal
				result = false;
			}
		}
		else
		{
			result = false;
		}
		// Display given number
		cout << "\n Given number a : " << a;
		cout << "\n Given number b : " << b;
		if (result)
		{
			cout << "\n Result : Yes \n";
		}
		else
		{
			cout << "\n Result : No \n";
		}
	}
};
int main()
{
	Permutation *task = new Permutation();
	// Test
	task->isEquivalent(123, 312);
	task->isEquivalent(12, 122);
	task->isEquivalent(1200, 1002);
	task->isEquivalent(1223, 3125);
	return 0;
}

Output

 Given number a : 123
 Given number b : 312
 Result : Yes

 Given number a : 12
 Given number b : 122
 Result : No

 Given number a : 1200
 Given number b : 1002
 Result : Yes

 Given number a : 1223
 Given number b : 3125
 Result : No
// Include namespace system
using System;
/*
    Csharp program for
    Check if permutation of a number is same to other number
*/
public class Permutation
{
	public int absValue(int x)
	{
		if (x < 0)
		{
			return -x;
		}
		return x;
	}
	public void isEquivalent(int a, int b)
	{
		Boolean result = true;
		// Make valid positive number
		int t1 = this.absValue(a);
		int t2 = this.absValue(b);
		// Convert number to character array 
		// Because It's suitable for sorting
		char[] s1 = ("" + t1).ToCharArray();
		char[] s2 = ("" + t2).ToCharArray();
		if (s1.Length == s2.Length)
		{
			// Sort both array
			Array.Sort(s1);
			Array.Sort(s2);
			// Convert character array to string
			String v1 = new String(s1);
			String v2 = new String(s2);
			if (!v1.Equals(v2))
			{
				// When both string is not equal
				result = false;
			}
		}
		else
		{
			result = false;
		}
		// Display given number
		Console.Write("\n Given number a : " + a);
		Console.Write("\n Given number b : " + b);
		if (result)
		{
			Console.Write("\n Result : Yes \n");
		}
		else
		{
			Console.Write("\n Result : No \n");
		}
	}
	public static void Main(String[] args)
	{
		Permutation task = new Permutation();
		// Test
		task.isEquivalent(123, 312);
		task.isEquivalent(12, 122);
		task.isEquivalent(1200, 1002);
		task.isEquivalent(1223, 3125);
	}
}

Output

 Given number a : 123
 Given number b : 312
 Result : Yes

 Given number a : 12
 Given number b : 122
 Result : No

 Given number a : 1200
 Given number b : 1002
 Result : Yes

 Given number a : 1223
 Given number b : 3125
 Result : No
package main
import "strings"
import "sort"
import "fmt"
import "strconv"
/*
    Go program for
    Check if permutation of a number is same to other number
*/


func absValue(x int) int {
	if x < 0 {
		return -x
	}
	return x
}
func SortString(str string) string {
    s := strings.Split(str, "")
    sort.Strings(s)
    return strings.Join(s, "")
}
func isEquivalent(a, b int) {
	var result bool = true
	// Make valid positive number
	var t1 int = absValue(a)
	var t2 int = absValue(b)
	var s1 = SortString(strconv.Itoa(t1))
	var s2 = SortString(strconv.Itoa(t2))

	if s1 != s2 {
		result = false
	}
	// Display given number
	fmt.Print("\n Given number a : ", a)
	fmt.Print("\n Given number b : ", b)
	if result {
		fmt.Print("\n Result : Yes \n")
	} else {
		fmt.Print("\n Result : No \n")
	}
}
func main() {

	// Test
	isEquivalent(123, 312)
	isEquivalent(12, 122)
	isEquivalent(1200, 1002)
	isEquivalent(1223, 3125)
}

Output

 Given number a : 123
 Given number b : 312
 Result : Yes

 Given number a : 12
 Given number b : 122
 Result : No

 Given number a : 1200
 Given number b : 1002
 Result : Yes

 Given number a : 1223
 Given number b : 3125
 Result : No
<?php
/*
    Php program for
    Check if permutation of a number is same to other number
*/
class Permutation
{
	public	function absValue($x)
	{
		if ($x < 0)
		{
			return -$x;
		}
		return $x;
	}
	public	function isEquivalent($a, $b)
	{
		$result = true;
		// Make valid positive number
		$t1 = $this->absValue($a);
		$t2 = $this->absValue($b);
		// Convert number to character array 
		// Because It's suitable for sorting
		$s1 = str_split(("".strval($t1)));
		$s2 = str_split(("".strval($t2)));
		if (count($s1) == count($s2))
		{
			// Sort both array
			sort($s1);
			sort($s2);
			// Convert character array to string
			$v1 = implode($s1);
			$v2 = implode($s2);
			if ((strcmp($v1, $v2) != 0))
			{
				// When both string is not equal
				$result = false;
			}
		}
		else
		{
			$result = false;
		}
		// Display given number
		echo("\n Given number a : ".$a);
		echo("\n Given number b : ".$b);
		if ($result)
		{
			echo("\n Result : Yes \n");
		}
		else
		{
			echo("\n Result : No \n");
		}
	}
}

function main()
{
	$task = new Permutation();
	// Test
	$task->isEquivalent(123, 312);
	$task->isEquivalent(12, 122);
	$task->isEquivalent(1200, 1002);
	$task->isEquivalent(1223, 3125);
}
main();

Output

 Given number a : 123
 Given number b : 312
 Result : Yes

 Given number a : 12
 Given number b : 122
 Result : No

 Given number a : 1200
 Given number b : 1002
 Result : Yes

 Given number a : 1223
 Given number b : 3125
 Result : No
/*
    Node JS program for
    Check if permutation of a number is same to other number
*/
class Permutation
{
	absValue(x)
	{
		if (x < 0)
		{
			return -x;
		}
		return x;
	}
	isEquivalent(a, b)
	{
		var result = true;
		// Make valid positive number
		var t1 = this.absValue(a);
		var t2 = this.absValue(b);
		// Convert number to character array 
		// Because It's suitable for sorting
		var s1 = (("" + t1)).split('').sort().join('');
		var s2 = (("" + t2)).split('').sort().join('');
		if (s1.length != s2.length || s1.localeCompare(s2) != 0)
		{
			// When both string is not equal
			result = false;
		}
		// Display given number
		process.stdout.write("\n Given number a : " + a);
		process.stdout.write("\n Given number b : " + b);
		if (result)
		{
			process.stdout.write("\n Result : Yes \n");
		}
		else
		{
			process.stdout.write("\n Result : No \n");
		}
	}
}

function main()
{
	var task = new Permutation();
	// Test
	task.isEquivalent(123, 312);
	task.isEquivalent(12, 122);
	task.isEquivalent(1200, 1002);
	task.isEquivalent(1223, 3125);
}
main();

Output

 Given number a : 123
 Given number b : 312
 Result : Yes

 Given number a : 12
 Given number b : 122
 Result : No

 Given number a : 1200
 Given number b : 1002
 Result : Yes

 Given number a : 1223
 Given number b : 3125
 Result : No
#    Python 3 program for
#    Check if permutation of a number is same to other number
class Permutation :
    def absValue(self, x) :
        if (x < 0) :
            return -x
        
        return x
    
    def isEquivalent(self, a, b) :
        result = True
        #  Make valid positive number
        t1 = self.absValue(a)
        t2 = self.absValue(b)
        s1 = ''.join(sorted(""+str(t1))) 
        s2 = ''.join(sorted(""+str(t2))) 
        if (len(s1) != len(s2) or s1 != s2) :
            #  When both string is not equal
            result = False
    
        
        #  Display given number
        print("\n Given number a : ", a, end = "")
        print("\n Given number b : ", b, end = "")
        if (result) :
            print("\n Result : Yes ")
        else :
            print("\n Result : No ")
        
    

def main() :
    task = Permutation()
    #  Test
    task.isEquivalent(123, 312)
    task.isEquivalent(12, 122)
    task.isEquivalent(1200, 1002)
    task.isEquivalent(1223, 3125)

if __name__ == "__main__": main()

Output

 Given number a : 123
 Given number b : 312
 Result : Yes

 Given number a : 12
 Given number b : 122
 Result : No

 Given number a : 1200
 Given number b : 1002
 Result : Yes

 Given number a : 1223
 Given number b : 3125
 Result : No
#    Ruby program for
#    Check if permutation of a number is same to other number
class Permutation 
	def absValue(x) 
		if (x < 0) 
			return -x
		end

		return x
	end

	def isEquivalent(a, b) 
		result = true
		#  Make valid positive number
		t1 = self.absValue(a)
		t2 = self.absValue(b)
		s1 = t1.to_s.chars.sort.join
		s2 = t2.to_s.chars.sort.join
		if (s1.length != s2.length || s1 != s2) 
			#  When both string is not equal
			result = false
		end

		#  Display given number
		print("\n Given number a : ", a)
		print("\n Given number b : ", b)
		if (result) 
			print("\n Result : Yes \n")
		else
			print("\n Result : No \n")
		end

	end

end

def main() 
	task = Permutation.new()
	#  Test
	task.isEquivalent(123, 312)
	task.isEquivalent(12, 122)
	task.isEquivalent(1200, 1002)
	task.isEquivalent(1223, 3125)
end

main()

Output

 Given number a : 123
 Given number b : 312
 Result : Yes 

 Given number a : 12
 Given number b : 122
 Result : No 

 Given number a : 1200
 Given number b : 1002
 Result : Yes 

 Given number a : 1223
 Given number b : 3125
 Result : No 
import scala.collection.mutable._;
/*
    Scala program for
    Check if permutation of a number is same to other number
*/
class Permutation()
{
	def absValue(x: Int): Int = {
		if (x < 0)
		{
			return -x;
		}
		return x;
	}
	def isEquivalent(a: Int, b: Int): Unit = {
		var result: Boolean = true;
		// Make valid positive number
		var t1: Int = absValue(a);
		var t2: Int = absValue(b);
		var s1: Array[Char] = ("" + t1.toString()).toCharArray();
		var s2: Array[Char] = ("" + t2.toString()).toCharArray();
		if (s1.length == s2.length)
		{
			// Sort both array
			s1 = s1.sorted;
			s2 = s2.sorted;
			// Convert character array to string
			var v1: String = new String(s1);
			var v2: String = new String(s2);
			if (!v1.equals(v2))
			{
				// When both string is not equal
				result = false;
			}
		}
		else
		{
			result = false;
		}
		// Display given number
		print("\n Given number a : " + a);
		print("\n Given number b : " + b);
		if (result)
		{
			print("\n Result : Yes \n");
		}
		else
		{
			print("\n Result : No \n");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Permutation = new Permutation();
		// Test
		task.isEquivalent(123, 312);
		task.isEquivalent(12, 122);
		task.isEquivalent(1200, 1002);
		task.isEquivalent(1223, 3125);
	}
}

Output

 Given number a : 123
 Given number b : 312
 Result : Yes

 Given number a : 12
 Given number b : 122
 Result : No

 Given number a : 1200
 Given number b : 1002
 Result : Yes

 Given number a : 1223
 Given number b : 3125
 Result : No
import Foundation;
/*
    Swift 4 program for
    Check if permutation of a number is same to other number
*/
class Permutation
{
	func absValue(_ x: Int) -> Int
	{
		if (x < 0)
		{
			return -x;
		}
		return x;
	}
	func isEquivalent(_ a: Int, _ b: Int)
	{
		var result: Bool = true;
		// Make valid positive number
		let t1: Int = self.absValue(a);
		let t2: Int = self.absValue(b);
		var s1: [Character] = Array(String(t1));
		var s2: [Character] = Array(String(t2));
		if (s1.count == s2.count)
		{
			// Sort both array
			s1 = s1.sorted();
			s2 = s2.sorted();
          	var i = 0;
          	var v1 = "";
          	var v2 = "";
			// Convert character array to string
			// Append text characters into ans    
            while (i < s1.count)
            {
                v1 += String(s1[i]);
              	v2 += String(s2[i]);
                i += 1;
            }
			if (v1 != v2)
			{
				// When both string is not equal
				result = false;
			}
		}
		else
		{
			result = false;
		}
		// Display given number
		print("\n Given number a : ", a, terminator: "");
		print("\n Given number b : ", b, terminator: "");
		if (result)
		{
			print("\n Result : Yes ");
		}
		else
		{
			print("\n Result : No ");
		}
	}
}
func main()
{
	let task: Permutation = Permutation();
	// Test
	task.isEquivalent(123, 312);
	task.isEquivalent(12, 122);
	task.isEquivalent(1200, 1002);
	task.isEquivalent(1223, 3125);
}
main();

Output

 Given number a :  123
 Given number b :  312
 Result : Yes

 Given number a :  12
 Given number b :  122
 Result : No

 Given number a :  1200
 Given number b :  1002
 Result : Yes

 Given number a :  1223
 Given number b :  3125
 Result : No
/*
    Kotlin program for
    Check if permutation of a number is same to other number
*/
class Permutation
{
	fun absValue(x: Int): Int
	{
		if (x < 0)
		{
			return -x;
		}
		return x;
	}
	fun isEquivalent(a: Int, b: Int): Unit
	{
		var result: Boolean = true;
		// Make valid positive number
		val t1: Int = this.absValue(a);
		val t2: Int = this.absValue(b);
		val s1: Array < Char > = t1.toString().toCharArray().toTypedArray();
		val s2: Array < Char > = t2.toString().toCharArray().toTypedArray();
		if (s1.count() == s2.count())
		{
			// Sort both array
			s1.sort();
			s2.sort();
			// Convert character array to string
			var v1 = "";
			var v2 = "";
			var i: Int = 0;
			// Append text characters into ans    
			while (i < s1.count())
			{
				v1 += s1[i];
				v2 += s2[i];
				i += 1;
			}
			if (!v1.equals(v2))
			{
				// When both string is not equal
				result = false;
			}
		}
		else
		{
			result = false;
		}
		// Display given number
		print("\n Given number a : " + a);
		print("\n Given number b : " + b);
		if (result)
		{
			print("\n Result : Yes \n");
		}
		else
		{
			print("\n Result : No \n");
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Permutation = Permutation();
	// Test
	task.isEquivalent(123, 312);
	task.isEquivalent(12, 122);
	task.isEquivalent(1200, 1002);
	task.isEquivalent(1223, 3125);
}

Output

 Given number a : 123
 Given number b : 312
 Result : Yes

 Given number a : 12
 Given number b : 122
 Result : No

 Given number a : 1200
 Given number b : 1002
 Result : Yes

 Given number a : 1223
 Given number b : 3125
 Result : No


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