[PHP] Text Permutation - Muhammad Faruq Nuruddinsyah
[PHP] Text Permutation

Text Permutation Generator using recursion technique in PHP

Sample input text:
ABC

Output:
ABC, ACB, BAC, BCA, CAB, CBA

Source Code:
<?php
 
// Text Permutation
// Faruq @ 2012
 
function createPermutationArray($str){
	$tmp = array();
 
	if(strlen($str) == 1){
		$tmp[] = $str;
	}
 
	for($i = 0; $i < strlen($str); $i++){
		$char = substr($str, $i, 1);
		$rchar = removeChar($str, $i);
 
		$p = createPermutationArray($rchar);
 
		for($j = 0; $j < count($p); $j++){
			$tmp[] = $char . $p[$j];
		}
	}
 
	return $tmp;
}
 
function removeChar($str, $index){
	$tmp = "";
	for($i = 0; $i < strlen($str); $i++){
		if($i != $index) $tmp .= substr($str, $i, 1);
	}
 
	return $tmp;
}
 
echo("Masukkan kata: "); $text = trim(fgets(STDIN)); echo "\n";
 
$result = createPermutationArray($text);
print_r($result);
 
?>

Informasi:
Lihat semua daftar ACS - Download: permutation.php - Tanggal: 3 April 2013 - Kategori: PHP