<?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);

?>