A string S is given. In one move, any two adjacent letters can be swapped. For example, given a string "abcd", it's possible to create "bacd", "acbd" or "abdc" in one such move. What is the lexicographically minimum string that can be achieved by at most K moves?
Write a function:
function solution($S, $K);
that, given a string S of length N and an integer K, returns the lexicographically minimum string that can be achieved by at most K swaps of any adjacent letters.
Examples:
1. Given S = "decade" and K = 4, your function should return "adcede". Swaps could be:
decade → dceade,
dceade → dcaede,
dcaede → dacede,
dacede → adcede.
2. Given S = "bbbabbb" and K = 2, your function should return "babbbbb". The swaps are:
bbbabbb → bbabbbb,
bbabbbb → babbbbb.
3. Given S = "abracadabra" and K = 15, your function should return "aaaaabrcdbr".
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [1..100,000];
- string S consists only of lowercase letters ('a'-'z');
- K is an integer within the range [0..1,000,000,000].
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($S, $K) {
// write your code in PHP7.0
}
<?php
function Swaps($S, $count, $K)
{
for ($a = 0; $a < $count-1 && $K > 0; $a++)
{
$position = $a;
for ($j = $a+1; $j < $count ; $j++)
{
if ($j-$a > $K)
break;
if ($S[$j] < $S[$position])
$position = $j;
}
for ($j = $position; $j > $a; $j--)
{
$temp = $S[$j];
$S[$j] = $S[$j-1];
$S[$j-1] = $temp;
}
$K -= $position-$a;
}
for ($a = 0; $a < $count; $a++)
echo $S[$a] . " ";
}
$string = "abracadabra";
$S = str_split($string);
$count = count($S);
$K = 15;
Swaps($S, $count, $K);
?>
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($S, $K) {
// write your code in PHP7.0
}
<?php
function Swaps($S, $count, $K)
{
for ($a = 0; $a < $count-1 && $K > 0; $a++)
{
$position = $a;
for ($j = $a+1; $j < $count ; $j++)
{
if ($j-$a > $K)
break;
if ($S[$j] < $S[$position])
$position = $j;
}
for ($j = $position; $j > $a; $j--)
{
$temp = $S[$j];
$S[$j] = $S[$j-1];
$S[$j-1] = $temp;
}
$K -= $position-$a;
}
for ($a = 0; $a < $count; $a++)
echo $S[$a] . " ";
}
$string = "abracadabra";
$S = str_split($string);
$K = 15;
Swaps($S, $count, $K);
?>
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($S, $K) {
// write your code in PHP7.0
}
<?php
function Swaps($S, $K)
{
$count = count($S);
for ($a = 0; $a < $count-1 && $K > 0; $a++)
{
$position = $a;
for ($j = $a+1; $j < $count ; $j++)
{
if ($j-$a > $K)
break;
if ($S[$j] < $S[$position])
$position = $j;
}
for ($j = $position; $j > $a; $j--)
{
$temp = $S[$j];
$S[$j] = $S[$j-1];
$S[$j-1] = $temp;
}
$K -= $position-$a;
}
for ($a = 0; $a < $count; $a++)
echo $S[$a] . " ";
}
$string = "abracadabra";
$S = str_split($string);
$K = 15;
Swaps($S, $count, $K);
?>
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function Swaps($S, $K)
{
$count = count($S);
for ($a = 0; $a < $count-1 && $K > 0; $a++)
{
$position = $a;
for ($j = $a+1; $j < $count ; $j++)
{
if ($j-$a > $K)
break;
if ($S[$j] < $S[$position])
$position = $j;
}
for ($j = $position; $j > $a; $j--)
{
$temp = $S[$j];
$S[$j] = $S[$j-1];
$S[$j-1] = $temp;
}
$K -= $position-$a;
}
for ($a = 0; $a < $count; $a++)
echo $S[$a] . " ";
}
$string = "abracadabra";
$S = str_split($string);
$K = 15;
Swaps($S, $count, $K);
?>
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function Swaps($S, $K)
{
$count = count($S);
for ($a = 0; $a < $count-1 && $K > 0; $a++)
{
$position = $a;
for ($j = $a+1; $j < $count ; $j++)
{
if ($j-$a > $K)
break;
if ($S[$j] < $S[$position])
$position = $j;
}
for ($j = $position; $j > $a; $j--)
{
$temp = $S[$j];
$S[$j] = $S[$j-1];
$S[$j-1] = $temp;
}
$K -= $position-$a;
}
for ($a = 0; $a < $count; $a++)
echo $S[$a] . " ";
}
PHP Fatal error: Uncaught Error: Call to undefined function solution() in /tmp/exec.php:137 Stack trace: #0 {main} thrown in /tmp/exec.php on line 137 Fatal error: Uncaught Error: Call to undefined function solution() in /tmp/exec.php:137 Stack trace: #0 {main} thrown in /tmp/exec.php on line 137
PHP Fatal error: Uncaught Error: Call to undefined function solution() in /tmp/exec.php:137 Stack trace: #0 {main} thrown in /tmp/exec.php on line 137 Fatal error: Uncaught Error: Call to undefined function solution() in /tmp/exec.php:137 Stack trace: #0 {main} thrown in /tmp/exec.php on line 137
PHP Fatal error: Uncaught Error: Call to undefined function solution() in /tmp/exec.php:137 Stack trace: #0 {main} thrown in /tmp/exec.php on line 137 Fatal error: Uncaught Error: Call to undefined function solution() in /tmp/exec.php:137 Stack trace: #0 {main} thrown in /tmp/exec.php on line 137
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function Swaps($S, $K)
{
$count = count($S);
for ($a = 0; $a < $count-1 && $K > 0; $a++)
{
$position = $a;
for ($j = $a+1; $j < $count ; $j++)
{
if ($j-$a > $K)
break;
if ($S[$j] < $S[$position])
$position = $j;
}
for ($j = $position; $j > $a; $j--)
{
$temp = $S[$j];
$S[$j] = $S[$j-1];
$S[$j-1] = $temp;
}
$K -= $position-$a;
}
for ($a = 0; $a < $count; $a++)
echo $S[$a] . " ";
}
$string = "abracadabra";
$S = str_split($string);
$K = 15;
Swaps($S, $count, $K);
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function Swaps($S, $K)
{
$count = count($S);
for ($a = 0; $a < $count-1 && $K > 0; $a++)
{
$position = $a;
for ($j = $a+1; $j < $count ; $j++)
{
if ($j-$a > $K)
break;
if ($S[$j] < $S[$position])
$position = $j;
}
for ($j = $position; $j > $a; $j--)
{
$temp = $S[$j];
$S[$j] = $S[$j-1];
$S[$j-1] = $temp;
}
$K -= $position-$a;
}
for ($a = 0; $a < $count; $a++)
echo $S[$a] . " ";
}
$S = str_split($string);
$K = 15;
Swaps($S, $count, $K);
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function Swaps($S, $K)
{
$count = count($S);
for ($a = 0; $a < $count-1 && $K > 0; $a++)
{
$position = $a;
for ($j = $a+1; $j < $count ; $j++)
{
if ($j-$a > $K)
break;
if ($S[$j] < $S[$position])
$position = $j;
}
for ($j = $position; $j > $a; $j--)
{
$temp = $S[$j];
$S[$j] = $S[$j-1];
$S[$j-1] = $temp;
}
$K -= $position-$a;
}
for ($a = 0; $a < $count; $a++)
echo $S[$a] . " ";
}
$S = str_split($string);
$K = 15;
Swaps($S, $count, $K);
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function Swaps($S, $K)
{
$S = str_split($string);
$count = count($S);
for ($a = 0; $a < $count-1 && $K > 0; $a++)
{
$position = $a;
for ($j = $a+1; $j < $count ; $j++)
{
if ($j-$a > $K)
break;
if ($S[$j] < $S[$position])
$position = $j;
}
for ($j = $position; $j > $a; $j--)
{
$temp = $S[$j];
$S[$j] = $S[$j-1];
$S[$j-1] = $temp;
}
$K -= $position-$a;
}
for ($a = 0; $a < $count; $a++)
echo $S[$a] . " ";
}
$S = str_split($string);
$K = 15;
Swaps($S, $count, $K);
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function Swaps($S, $K)
{
$S = str_split($string);
$count = count($S);
for ($a = 0; $a < $count-1 && $K > 0; $a++)
{
$position = $a;
for ($j = $a+1; $j < $count ; $j++)
{
if ($j-$a > $K)
break;
if ($S[$j] < $S[$position])
$position = $j;
}
for ($j = $position; $j > $a; $j--)
{
$temp = $S[$j];
$S[$j] = $S[$j-1];
$S[$j-1] = $temp;
}
$K -= $position-$a;
}
for ($a = 0; $a < $count; $a++)
echo $S[$a] . " ";
}
$K = 15;
Swaps($S, $count, $K);
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function Swaps($S, $K)
{
$S = str_split($string);
$count = count($S);
for ($a = 0; $a < $count-1 && $K > 0; $a++)
{
$position = $a;
for ($j = $a+1; $j < $count ; $j++)
{
if ($j-$a > $K)
break;
if ($S[$j] < $S[$position])
$position = $j;
}
for ($j = $position; $j > $a; $j--)
{
$temp = $S[$j];
$S[$j] = $S[$j-1];
$S[$j-1] = $temp;
}
$K -= $position-$a;
}
for ($a = 0; $a < $count; $a++)
echo $S[$a] . " ";
}
Swaps($S, $count, $K);
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function Swaps($S, $K)
{
$S = str_split($string);
$count = count($S);
for ($a = 0; $a < $count-1 && $K > 0; $a++)
{
$position = $a;
for ($j = $a+1; $j < $count ; $j++)
{
if ($j-$a > $K)
break;
if ($S[$j] < $S[$position])
$position = $j;
}
for ($j = $position; $j > $a; $j--)
{
$temp = $S[$j];
$S[$j] = $S[$j-1];
$S[$j-1] = $temp;
}
$K -= $position-$a;
}
for ($a = 0; $a < $count; $a++)
echo $S[$a] . " ";
}
Swaps($S, $K);
PHP Notice: Undefined variable: S in func.php on line 39 Notice: Undefined variable: S in func.php on line 39 PHP Notice: Undefined variable: K in func.php on line 39 Notice: Undefined variable: K in func.php on line 39 PHP Notice: Undefined variable: string in func.php on line 6 Notice: Undefined variable: string in func.php on line 6 PHP Fatal error: Uncaught Error: Call to undefined function solution() in /tmp/exec.php:140 Stack trace: #0 {main} thrown in /tmp/exec.php on line 140 Fatal error: Uncaught Error: Call to undefined function solution() in /tmp/exec.php:140 Stack trace: #0 {main} thrown in /tmp/exec.php on line 140stdout:
PHP Notice: Undefined variable: S in func.php on line 39 Notice: Undefined variable: S in func.php on line 39 PHP Notice: Undefined variable: K in func.php on line 39 Notice: Undefined variable: K in func.php on line 39 PHP Notice: Undefined variable: string in func.php on line 6 Notice: Undefined variable: string in func.php on line 6 PHP Fatal error: Uncaught Error: Call to undefined function solution() in /tmp/exec.php:140 Stack trace: #0 {main} thrown in /tmp/exec.php on line 140 Fatal error: Uncaught Error: Call to undefined function solution() in /tmp/exec.php:140 Stack trace: #0 {main} thrown in /tmp/exec.php on line 140stdout:
PHP Notice: Undefined variable: S in func.php on line 39 Notice: Undefined variable: S in func.php on line 39 PHP Notice: Undefined variable: K in func.php on line 39 Notice: Undefined variable: K in func.php on line 39 PHP Notice: Undefined variable: string in func.php on line 6 Notice: Undefined variable: string in func.php on line 6 PHP Fatal error: Uncaught Error: Call to undefined function solution() in /tmp/exec.php:140 Stack trace: #0 {main} thrown in /tmp/exec.php on line 140 Fatal error: Uncaught Error: Call to undefined function solution() in /tmp/exec.php:140 Stack trace: #0 {main} thrown in /tmp/exec.php on line 140stdout:
$string = "abracadabra";
$K = 15;
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function Swaps($S, $K)
{
$S = str_split($string);
$count = count($S);
for ($a = 0; $a < $count-1 && $K > 0; $a++)
{
$position = $a;
for ($j = $a+1; $j < $count ; $j++)
{
if ($j-$a > $K)
break;
if ($S[$j] < $S[$position])
$position = $j;
}
for ($j = $position; $j > $a; $j--)
{
$temp = $S[$j];
$S[$j] = $S[$j-1];
$S[$j-1] = $temp;
}
$K -= $position-$a;
}
for ($a = 0; $a < $count; $a++)
echo $S[$a] . " ";
}
Swaps($S, $K);
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function Swaps($S, $K)
{
$S = str_split($string);
$count = count($S);
for ($a = 0; $a < $count-1 && $K > 0; $a++)
{
$position = $a;
for ($j = $a+1; $j < $count ; $j++)
{
if ($j-$a > $K)
break;
if ($S[$j] < $S[$position])
$position = $j;
}
for ($j = $position; $j > $a; $j--)
{
$temp = $S[$j];
$S[$j] = $S[$j-1];
$S[$j-1] = $temp;
}
$K -= $position-$a;
}
for ($a = 0; $a < $count; $a++)
echo $S[$a] . " ";
}
Swaps($S, $K);
PHP Notice: Undefined variable: S in func.php on line 39 Notice: Undefined variable: S in func.php on line 39 PHP Notice: Undefined variable: K in func.php on line 39 Notice: Undefined variable: K in func.php on line 39 PHP Notice: Undefined variable: string in func.php on line 6 Notice: Undefined variable: string in func.php on line 6 PHP Fatal error: Uncaught Error: Call to undefined function solution() in /tmp/exec.php:140 Stack trace: #0 {main} thrown in /tmp/exec.php on line 140 Fatal error: Uncaught Error: Call to undefined function solution() in /tmp/exec.php:140 Stack trace: #0 {main} thrown in /tmp/exec.php on line 140stdout:
PHP Notice: Undefined variable: S in func.php on line 39 Notice: Undefined variable: S in func.php on line 39 PHP Notice: Undefined variable: K in func.php on line 39 Notice: Undefined variable: K in func.php on line 39 PHP Notice: Undefined variable: string in func.php on line 6 Notice: Undefined variable: string in func.php on line 6 PHP Fatal error: Uncaught Error: Call to undefined function solution() in /tmp/exec.php:140 Stack trace: #0 {main} thrown in /tmp/exec.php on line 140 Fatal error: Uncaught Error: Call to undefined function solution() in /tmp/exec.php:140 Stack trace: #0 {main} thrown in /tmp/exec.php on line 140stdout:
PHP Notice: Undefined variable: S in func.php on line 39 Notice: Undefined variable: S in func.php on line 39 PHP Notice: Undefined variable: K in func.php on line 39 Notice: Undefined variable: K in func.php on line 39 PHP Notice: Undefined variable: string in func.php on line 6 Notice: Undefined variable: string in func.php on line 6 PHP Fatal error: Uncaught Error: Call to undefined function solution() in /tmp/exec.php:140 Stack trace: #0 {main} thrown in /tmp/exec.php on line 140 Fatal error: Uncaught Error: Call to undefined function solution() in /tmp/exec.php:140 Stack trace: #0 {main} thrown in /tmp/exec.php on line 140stdout:
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function Swaps($S, $K)
{
$S = str_split($string);
$count = count($S);
for ($a = 0; $a < $count-1 && $K > 0; $a++)
{
$position = $a;
for ($j = $a+1; $j < $count ; $j++)
{
if ($j-$a > $K)
break;
if ($S[$j] < $S[$position])
$position = $j;
}
for ($j = $position; $j > $a; $j--)
{
$temp = $S[$j];
$S[$j] = $S[$j-1];
$S[$j-1] = $temp;
}
$K -= $position-$a;
}
for ($a = 0; $a < $count; $a++)
echo $S[$a] . " ";
}
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function Swaps($S, $K)
{
$S = str_split($string);
$count = count($S);
for ($a = 0; $a < $count-1 && $K > 0; $a++)
{
$position = $a;
for ($j = $a+1; $j < $count ; $j++)
{
if ($j-$a > $K)
break;
if ($S[$j] < $S[$position])
$position = $j;
}
for ($j = $position; $j > $a; $j--)
{
$temp = $S[$j];
$S[$j] = $S[$j-1];
$S[$j-1] = $temp;
}
$K -= $position-$a;
}
for ($a = 0; $a < $count; $a++)
echo $S[$a] . " ";
}
PHP Fatal error: Uncaught Error: Call to undefined function solution() in /tmp/exec.php:139 Stack trace: #0 {main} thrown in /tmp/exec.php on line 139 Fatal error: Uncaught Error: Call to undefined function solution() in /tmp/exec.php:139 Stack trace: #0 {main} thrown in /tmp/exec.php on line 139
PHP Fatal error: Uncaught Error: Call to undefined function solution() in /tmp/exec.php:139 Stack trace: #0 {main} thrown in /tmp/exec.php on line 139 Fatal error: Uncaught Error: Call to undefined function solution() in /tmp/exec.php:139 Stack trace: #0 {main} thrown in /tmp/exec.php on line 139
PHP Fatal error: Uncaught Error: Call to undefined function solution() in /tmp/exec.php:139 Stack trace: #0 {main} thrown in /tmp/exec.php on line 139 Fatal error: Uncaught Error: Call to undefined function solution() in /tmp/exec.php:139 Stack trace: #0 {main} thrown in /tmp/exec.php on line 139
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($S, $K)
{
$S = str_split($string);
$count = count($S);
for ($a = 0; $a < $count-1 && $K > 0; $a++)
{
$position = $a;
for ($j = $a+1; $j < $count ; $j++)
{
if ($j-$a > $K)
break;
if ($S[$j] < $S[$position])
$position = $j;
}
for ($j = $position; $j > $a; $j--)
{
$temp = $S[$j];
$S[$j] = $S[$j-1];
$S[$j-1] = $temp;
}
$K -= $position-$a;
}
for ($a = 0; $a < $count; $a++)
echo $S[$a] . " ";
}
PHP Notice: Undefined variable: string in func.php on line 6 Notice: Undefined variable: string in func.php on line 6 Invalid result type, string expected (got NULL)stdout:
PHP Notice: Undefined variable: string in func.php on line 6 Notice: Undefined variable: string in func.php on line 6 Invalid result type, string expected (got NULL)stdout:
PHP Notice: Undefined variable: string in func.php on line 6 Notice: Undefined variable: string in func.php on line 6 Invalid result type, string expected (got NULL)stdout:
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($S, $K)
{
$S = str_split($strin);
$count = count($S);
for ($a = 0; $a < $count-1 && $K > 0; $a++)
{
$position = $a;
for ($j = $a+1; $j < $count ; $j++)
{
if ($j-$a > $K)
break;
if ($S[$j] < $S[$position])
$position = $j;
}
for ($j = $position; $j > $a; $j--)
{
$temp = $S[$j];
$S[$j] = $S[$j-1];
$S[$j-1] = $temp;
}
$K -= $position-$a;
}
for ($a = 0; $a < $count; $a++)
echo $S[$a] . " ";
}
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($S, $K)
{
$S = str_split($S);
$count = count($S);
for ($a = 0; $a < $count-1 && $K > 0; $a++)
{
$position = $a;
for ($j = $a+1; $j < $count ; $j++)
{
if ($j-$a > $K)
break;
if ($S[$j] < $S[$position])
$position = $j;
}
for ($j = $position; $j > $a; $j--)
{
$temp = $S[$j];
$S[$j] = $S[$j-1];
$S[$j-1] = $temp;
}
$K -= $position-$a;
}
for ($a = 0; $a < $count; $a++)
echo $S[$a] . " ";
}
Invalid result type, string expected (got NULL)stdout:
a d c e d e
Invalid result type, string expected (got NULL)stdout:
b a b b b b b
Invalid result type, string expected (got NULL)stdout:
a a a a a b r c d b r
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($S, $K)
{
$S = str_split($S);
$count = count($S);
for ($a = 0; $a < $count-1 && $K > 0; $a++)
{
$position = $a;
for ($j = $a+1; $j < $count ; $j++)
{
if ($j-$a > $K)
break;
if ($S[$j] < $S[$position])
$position = $j;
}
for ($j = $position; $j > $a; $j--)
{
$temp = $S[$j];
$S[$j] = $S[$j-1];
$S[$j-1] = $temp;
}
$K -= $position-$a;
}
for ($a = 0; $a < $count; $a++)
echo $S[$a] . " ";
}
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($S, $K)
{
$S = str_split($S);
$count = count($S);
for ($a = 0; $a < $count-1 && $K > 0; $a++)
{
$position = $a;
for ($j = $a+1; $j < $count ; $j++)
{
if ($j-$a > $K)
break;
if ($S[$j] < $S[$position])
$position = $j;
}
for ($j = $position; $j > $a; $j--)
{
$temp = $S[$j];
$S[$j] = $S[$j-1];
$S[$j-1] = $temp;
}
$K -= $position-$a;
}
for ($a = 0; $a < $count; $a++)
echo $S[$a] . " ";
}
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($S, $K)
{
$S = str_split($S);
$count = count($S);
for ($a = 0; $a < $count-1 && $K > 0; $a++)
{
$position = $a;
for ($j = $a+1; $j < $count ; $j++)
{
if ($j-$a > $K)
break;
if ($S[$j] < $S[$position])
$position = $j;
}
for ($j = $position; $j > $a; $j--)
{
$temp = $S[$j];
$S[$j] = $S[$j-1];
$S[$j-1] = $temp;
}
$K -= $position-$a;
}
for ($a = 0; $a < $count; $a++)
echo $S[$a] . " ";
}
Invalid result type, string expected (got NULL)stdout:
a d c e d e
Invalid result type, string expected (got NULL)stdout:
b a b b b b b
Invalid result type, string expected (got NULL)stdout:
a a a a a b r c d b r
Invalid result type, string expected (got NULL)stdout:
a d c e d e
['decade', 4]
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($S, $K)
{
$S = str_split($S);
$count = count($S);
for ($a = 0; $a < $count-1 && $K > 0; $a++)
{
$position = $a;
for ($j = $a+1; $j < $count ; $j++)
{
if ($j-$a > $K)
break;
if ($S[$j] < $S[$position])
$position = $j;
}
for ($j = $position; $j > $a; $j--)
{
$temp = $S[$j];
$S[$j] = $S[$j-1];
$S[$j-1] = $temp;
}
$K -= $position-$a;
}
for ($a = 0; $a < $count; $a++)
//echo $S[$a] . " ";
}
PHP Parse error: syntax error, unexpected '}' in func.php on line 36 Parse error: syntax error, unexpected '}' in func.php on line 36
PHP Parse error: syntax error, unexpected '}' in func.php on line 36 Parse error: syntax error, unexpected '}' in func.php on line 36
PHP Parse error: syntax error, unexpected '}' in func.php on line 36 Parse error: syntax error, unexpected '}' in func.php on line 36
PHP Parse error: syntax error, unexpected '}' in func.php on line 36 Parse error: syntax error, unexpected '}' in func.php on line 36
['decade', 4]
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($S, $K)
{
$S = str_split($S);
$count = count($S);
for ($a = 0; $a < $count-1 && $K > 0; $a++)
{
$position = $a;
for ($j = $a+1; $j < $count ; $j++)
{
if ($j-$a > $K)
break;
if ($S[$j] < $S[$position])
$position = $j;
}
for ($j = $position; $j > $a; $j--)
{
$temp = $S[$j];
$S[$j] = $S[$j-1];
$S[$j-1] = $temp;
}
$K -= $position-$a;
}
for ($a = 0; $a < $count; $a++){
//echo $S[$a] . " ";
}
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($S, $K)
{
$S = str_split($S);
$count = count($S);
for ($a = 0; $a < $count-1 && $K > 0; $a++)
{
$position = $a;
for ($j = $a+1; $j < $count ; $j++)
{
if ($j-$a > $K)
break;
if ($S[$j] < $S[$position])
$position = $j;
}
for ($j = $position; $j > $a; $j--)
{
$temp = $S[$j];
$S[$j] = $S[$j-1];
$S[$j-1] = $temp;
}
$K -= $position-$a;
}
// for ($a = 0; $a < $count; $a++){
// //echo $S[$a] . " ";
// }
PHP Parse error: syntax error, unexpected end of file in /tmp/exec.php on line 142 Parse error: syntax error, unexpected end of file in /tmp/exec.php on line 142
PHP Parse error: syntax error, unexpected end of file in /tmp/exec.php on line 142 Parse error: syntax error, unexpected end of file in /tmp/exec.php on line 142
PHP Parse error: syntax error, unexpected end of file in /tmp/exec.php on line 142 Parse error: syntax error, unexpected end of file in /tmp/exec.php on line 142
PHP Parse error: syntax error, unexpected end of file in /tmp/exec.php on line 142 Parse error: syntax error, unexpected end of file in /tmp/exec.php on line 142
['decade', 4]
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($S, $K)
{
$S = str_split($S);
$count = count($S);
for ($a = 0; $a < $count-1 && $K > 0; $a++)
{
$position = $a;
for ($j = $a+1; $j < $count ; $j++)
{
if ($j-$a > $K)
break;
if ($S[$j] < $S[$position])
$position = $j;
}
for ($j = $position; $j > $a; $j--)
{
$temp = $S[$j];
$S[$j] = $S[$j-1];
$S[$j-1] = $temp;
}
$K -= $position-$a;
}
// for ($a = 0; $a < $count; $a++){
// //echo $S[$a] . " ";
}
Invalid result type, string expected (got NULL)
Invalid result type, string expected (got NULL)
Invalid result type, string expected (got NULL)
Invalid result type, string expected (got NULL)
['decade', 4]
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(S, K):
# write your code in Python 3.6
pass
# Python 3 program to find the new string
# after performing deletions and append
# operation in the string s
# Function to find the new string thus
# formed by removing characters
def newString(s, k):
# new string
X = ""
# Remove characters until
# the string is empty
while (len(s) > 0):
temp = s[0]
# Traverse to find the smallest
# character in the first k characters
i = 1
while(i < k+2 and i < len(s)):
if (s[i] < temp):
temp = s[i]
i += 1
# append the smallest character
X = X + temp
# removing the lexicographically
# smallest character from the string
for i in range(k+1):
if (s[i] == temp):
s = s[0:i] + s[i + 1:]
break
return X
# Driver Code
if __name__ == '__main__':
s = "decade"
k = 1
print(newString(s, k))
# This code is contributed by
# Shashank_Sharma
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(S, K):
# new string
X = ""
# Remove characters until
# the string is empty
while (len(s) > 0):
temp = s[0]
# Traverse to find the smallest
# character in the first k characters
i = 1
while(i < k+2 and i < len(s)):
if (s[i] < temp):
temp = s[i]
i += 1
# append the smallest character
X = X + temp
# removing the lexicographically
# smallest character from the string
for i in range(k+1):
if (s[i] == temp):
s = s[0:i] + s[i + 1:]
break
return X
# Driver Code
if __name__ == '__main__':
s = "decade"
k = 1
print(newString(s, k))
# This code is contributed by
# Shashank_Sharma
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(S, K):
# new string
X = ""
# Remove characters until
# the string is empty
while (len(s) > 0):
temp = s[0]
# Traverse to find the smallest
# character in the first k characters
i = 1
while(i < k+2 and i < len(s)):
if (s[i] < temp):
temp = s[i]
i += 1
# append the smallest character
X = X + temp
# removing the lexicographically
# smallest character from the string
for i in range(k+1):
if (s[i] == temp):
s = s[0:i] + s[i + 1:]
break
return X
# Driver Code
# if __name__ == '__main__':
# s = "decade"
# k = 1
# print(newString(s, k))
# This code is contributed by
# Shashank_Sharma
Traceback (most recent call last): File "exec.py", line 127, in <module> main() File "exec.py", line 89, in main result = solution( S, K ) File "/tmp/solution.py", line 12, in solution while (len(s) > 0): UnboundLocalError: local variable 's' referenced before assignment
Traceback (most recent call last): File "exec.py", line 127, in <module> main() File "exec.py", line 89, in main result = solution( S, K ) File "/tmp/solution.py", line 12, in solution while (len(s) > 0): UnboundLocalError: local variable 's' referenced before assignment
Traceback (most recent call last): File "exec.py", line 127, in <module> main() File "exec.py", line 89, in main result = solution( S, K ) File "/tmp/solution.py", line 12, in solution while (len(s) > 0): UnboundLocalError: local variable 's' referenced before assignment
Traceback (most recent call last): File "exec.py", line 127, in <module> main() File "exec.py", line 89, in main result = solution( S, K ) File "/tmp/solution.py", line 12, in solution while (len(s) > 0): UnboundLocalError: local variable 's' referenced before assignment
['decade', 4]
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($S, $K)
{
$S = str_split($S);
$count = count($S);
for ($a = 0; $a < $count-1 && $K > 0; $a++)
{
$position = $a;
for ($j = $a+1; $j < $count ; $j++)
{
if ($j-$a > $K)
break;
if ($S[$j] < $S[$position])
$position = $j;
}
for ($j = $position; $j > $a; $j--)
{
$temp = $S[$j];
$S[$j] = $S[$j-1];
$S[$j-1] = $temp;
}
$K -= $position-$a;
}
// for ($a = 0; $a < $count; $a++){
// //echo $S[$a] . " ";
}
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($S, $K)
{
$S1 = str_split($S);
$count = count($S);
for ($a = 0; $a < $count-1 && $K > 0; $a++)
{
$position = $a;
for ($j = $a+1; $j < $count ; $j++)
{
if ($j-$a > $K)
break;
if ($S[$j] < $S[$position])
$position = $j;
}
for ($j = $position; $j > $a; $j--)
{
$temp = $S[$j];
$S[$j] = $S[$j-1];
$S[$j-1] = $temp;
}
$K -= $position-$a;
}
// for ($a = 0; $a < $count; $a++){
// //echo $S[$a] . " ";
}
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($S, $K)
{
$S1 = str_split($S);
$count = count($S1);
for ($a = 0; $a < $count-1 && $K > 0; $a++)
{
$position = $a;
for ($j = $a+1; $j < $count ; $j++)
{
if ($j-$a > $K)
break;
if ($S[$j] < $S[$position])
$position = $j;
}
for ($j = $position; $j > $a; $j--)
{
$temp = $S[$j];
$S[$j] = $S[$j-1];
$S[$j-1] = $temp;
}
$K -= $position-$a;
}
// for ($a = 0; $a < $count; $a++){
// //echo $S[$a] . " ";
}
Invalid result type, string expected (got NULL)
Invalid result type, string expected (got NULL)
Invalid result type, string expected (got NULL)
Invalid result type, string expected (got NULL)
['decade', 4]
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($S, $K)
{
$S = str_split($S);
$count = count($S);
for ($a = 0; $a < $count-1 && $K > 0; $a++)
{
$position = $a;
for ($j = $a+1; $j < $count ; $j++)
{
if ($j-$a > $K)
break;
if ($S[$j] < $S[$position])
$position = $j;
}
for ($j = $position; $j > $a; $j--)
{
$temp = $S[$j];
$S[$j] = $S[$j-1];
$S[$j-1] = $temp;
}
$K -= $position-$a;
}
// for ($a = 0; $a < $count; $a++){
// //echo $S[$a] . " ";
}
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($S, $K)
{
$S = str_split($S);
$count = count($S);
for ($a = 0; $a < $count-1 && $K > 0; $a++)
{
$position = $a;
for ($j = $a+1; $j < $count ; $j++)
{
if ($j-$a > $K)
break;
if ($S[$j] < $S[$position])
$position = $j;
}
for ($j = $position; $j > $a; $j--)
{
$temp = $S[$j];
$S[$j] = $S[$j-1];
$S[$j-1] = $temp;
}
$K -= $position-$a;
}
retun;
// for ($a = 0; $a < $count; $a++){
// //echo $S[$a] . " ";
}
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($S, $K)
{
$S = str_split($S);
$count = count($S);
for ($a = 0; $a < $count-1 && $K > 0; $a++)
{
$position = $a;
for ($j = $a+1; $j < $count ; $j++)
{
if ($j-$a > $K)
break;
if ($S[$j] < $S[$position])
$position = $j;
}
for ($j = $position; $j > $a; $j--)
{
$temp = $S[$j];
$S[$j] = $S[$j-1];
$S[$j-1] = $temp;
}
$K -= $position-$a;
}
retun;
// for ($a = 0; $a < $count; $a++){
// //echo $S[$a] . " ";
}
PHP Notice: Use of undefined constant retun - assumed 'retun' in func.php on line 33 Notice: Use of undefined constant retun - assumed 'retun' in func.php on line 33 Invalid result type, string expected (got NULL)
PHP Notice: Use of undefined constant retun - assumed 'retun' in func.php on line 33 Notice: Use of undefined constant retun - assumed 'retun' in func.php on line 33 Invalid result type, string expected (got NULL)
PHP Notice: Use of undefined constant retun - assumed 'retun' in func.php on line 33 Notice: Use of undefined constant retun - assumed 'retun' in func.php on line 33 Invalid result type, string expected (got NULL)
PHP Notice: Use of undefined constant retun - assumed 'retun' in func.php on line 33 Notice: Use of undefined constant retun - assumed 'retun' in func.php on line 33 Invalid result type, string expected (got NULL)
['decade', 4]
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($S, $K)
{
$S = str_split($S);
$count = count($S);
for ($a = 0; $a < $count-1 && $K > 0; $a++)
{
$position = $a;
for ($j = $a+1; $j < $count ; $j++)
{
if ($j-$a > $K)
break;
if ($S[$j] < $S[$position])
$position = $j;
}
for ($j = $position; $j > $a; $j--)
{
$temp = $S[$j];
$S[$j] = $S[$j-1];
$S[$j-1] = $temp;
}
$K -= $position-$a;
}
return;
// for ($a = 0; $a < $count; $a++){
// //echo $S[$a] . " ";
}
Invalid result type, string expected (got NULL)
Invalid result type, string expected (got NULL)
Invalid result type, string expected (got NULL)
Invalid result type, string expected (got NULL)
['decade', 4]
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($S, $K)
{
$S = str_split($S);
$count = count($S);
for ($a = 0; $a < $count-1 && $K > 0; $a++)
{
$position = $a;
for ($j = $a+1; $j < $count ; $j++)
{
if ($j-$a > $K)
break;
if ($S[$j] < $S[$position])
$position = $j;
}
for ($j = $position; $j > $a; $j--)
{
$temp = $S[$j];
$S[$j] = $S[$j-1];
$S[$j-1] = $temp;
}
$K -= $position-$a;
}
return($S[$a]);
// for ($a = 0; $a < $count; $a++){
// //echo $S[$a] . " ";
}
['decade', 4]
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($S, $K)
{
$S = str_split($S);
$count = count($S);
for ($a = 0; $a < $count-1 && $K > 0; $a++)
{
$position = $a;
for ($j = $a+1; $j < $count ; $j++)
{
if ($j-$a > $K)
break;
if ($S[$j] < $S[$position])
$position = $j;
}
for ($j = $position; $j > $a; $j--)
{
$temp = $S[$j];
$S[$j] = $S[$j-1];
$S[$j-1] = $temp;
}
$K -= $position-$a;
}
// for ($a = 0; $a < $count; $a++){
// //echo $S[$a] . " ";
}
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($S, $K)
{
$S = str_split($S);
$count = count($S);
for ($a = 0; $a < $count-1 && $K > 0; $a++)
{
$position = $a;
for ($j = $a+1; $j < $count ; $j++)
{
if ($j-$a > $K)
break;
if ($S[$j] < $S[$position])
$position = $j;
}
for ($j = $position; $j > $a; $j--)
{
$temp = $S[$j];
$S[$j] = $S[$j-1];
$S[$j-1] = $temp;
}
$K -= $position-$a;
}
for ($a = 0; $a < $count; $a++){
return($S[$a]);
//echo $S[$a] . " ";
}
PHP Parse error: syntax error, unexpected end of file in /tmp/exec.php on line 143 Parse error: syntax error, unexpected end of file in /tmp/exec.php on line 143
PHP Parse error: syntax error, unexpected end of file in /tmp/exec.php on line 143 Parse error: syntax error, unexpected end of file in /tmp/exec.php on line 143
PHP Parse error: syntax error, unexpected end of file in /tmp/exec.php on line 143 Parse error: syntax error, unexpected end of file in /tmp/exec.php on line 143
PHP Parse error: syntax error, unexpected end of file in /tmp/exec.php on line 143 Parse error: syntax error, unexpected end of file in /tmp/exec.php on line 143
['decade', 4]
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($S, $K)
{
$S = str_split($S);
$count = count($S);
for ($a = 0; $a < $count-1 && $K > 0; $a++)
{
$position = $a;
for ($j = $a+1; $j < $count ; $j++)
{
if ($j-$a > $K)
break;
if ($S[$j] < $S[$position])
$position = $j;
}
for ($j = $position; $j > $a; $j--)
{
$temp = $S[$j];
$S[$j] = $S[$j-1];
$S[$j-1] = $temp;
}
$K -= $position-$a;
}
for ($a = 0; $a < $count; $a++){
return($S[$a]);
//echo $S[$a] . " ";
}
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($S, $K)
{
$S = str_split($S);
$count = count($S);
for ($a = 0; $a < $count-1 && $K > 0; $a++)
{
$position = $a;
for ($j = $a+1; $j < $count ; $j++)
{
if ($j-$a > $K)
break;
if ($S[$j] < $S[$position])
$position = $j;
}
for ($j = $position; $j > $a; $j--)
{
$temp = $S[$j];
$S[$j] = $S[$j-1];
$S[$j-1] = $temp;
}
$K -= $position-$a;
}
for ($a = 0; $a < $count; $a++){
return($S[$a]);
//echo $S[$a] . " ";
}
}
['decade', 4]
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($S, $K)
{
$S = str_split($S);
$count = count($S);
for ($a = 0; $a < $count-1 && $K > 0; $a++)
{
$position = $a;
for ($j = $a+1; $j < $count ; $j++)
{
if ($j-$a > $K)
break;
if ($S[$j] < $S[$position])
$position = $j;
}
for ($j = $position; $j > $a; $j--)
{
$temp = $S[$j];
$S[$j] = $S[$j-1];
$S[$j-1] = $temp;
}
$K -= $position-$a;
}
for ($a = 0; $a < $count; $a++){
return($S[$a]);
//echo $S[$a] . " ";
}
}
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($S, $K)
{
$S = str_split($S);
$count = count($S);
for ($a = 0; $a < $count-1 && $K > 0; $a++)
{
$position = $a;
for ($j = $a+1; $j < $count ; $j++)
{
if ($j-$a > $K)
break;
if ($S[$j] < $S[$position])
$position = $j;
}
for ($j = $position; $j > $a; $j--)
{
$temp = $S[$j];
$S[$j] = $S[$j-1];
$S[$j-1] = $temp;
}
$K -= $position-$a;
}
for ($a = 0; $a < $count; $a++){
return($S[$a]);
//echo $S[$a] . " ";
}
}
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($S, $K)
{
$S = str_split($S);
$count = count($S);
for ($a = 0; $a < $count-1 && $K > 0; $a++)
{
$position = $a;
for ($j = $a+1; $j < $count ; $j++)
{
if ($j-$a > $K)
break;
if ($S[$j] < $S[$position])
$position = $j;
}
for ($j = $position; $j > $a; $j--)
{
$temp = $S[$j];
$S[$j] = $S[$j-1];
$S[$j-1] = $temp;
}
$K -= $position-$a;
}
for ($a = 0; $a < $count; $a++){
return($S[$a]);
//echo $S[$a] . " ";
}
}
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($S, $K)
{
$S = str_split($S);
print $S;
$count = count($S);
for ($a = 0; $a < $count-1 && $K > 0; $a++)
{
$position = $a;
for ($j = $a+1; $j < $count ; $j++)
{
if ($j-$a > $K)
break;
if ($S[$j] < $S[$position])
$position = $j;
}
for ($j = $position; $j > $a; $j--)
{
$temp = $S[$j];
$S[$j] = $S[$j-1];
$S[$j-1] = $temp;
}
$K -= $position-$a;
}
for ($a = 0; $a < $count; $a++){
return($S[$a]);
//echo $S[$a] . " ";
}
}
PHP Notice: Array to string conversion in func.php on line 7 Notice: Array to string conversion in func.php on line 7stdout:
Array
PHP Notice: Array to string conversion in func.php on line 7 Notice: Array to string conversion in func.php on line 7stdout:
Array
PHP Notice: Array to string conversion in func.php on line 7 Notice: Array to string conversion in func.php on line 7stdout:
Array
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($S, $K)
{
$S = str_split($S);
print_r($S);
$count = count($S);
for ($a = 0; $a < $count-1 && $K > 0; $a++)
{
$position = $a;
for ($j = $a+1; $j < $count ; $j++)
{
if ($j-$a > $K)
break;
if ($S[$j] < $S[$position])
$position = $j;
}
for ($j = $position; $j > $a; $j--)
{
$temp = $S[$j];
$S[$j] = $S[$j-1];
$S[$j-1] = $temp;
}
$K -= $position-$a;
}
for ($a = 0; $a < $count; $a++){
return($S[$a]);
//echo $S[$a] . " ";
}
}
Array ( [0] => d [1] => e [2] => c [3] => a [4] => d [5] => e )
Array ( [0] => b [1] => b [2] => b [3] => a [4] => b [5] => b [6] => b )
Array ( [0] => a [1] => b [2] => r [3] => a [4] => c [5] => a [6] => d [7] => a [8] => b [9] => r [10] => a )
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($S, $K)
{
$S = str_split($S);
print_r($S);
$count = count($S);
for ($a = 0; $a < $count-1 && $K > 0; $a++)
{
$position = $a;
for ($j = $a+1; $j < $count ; $j++)
{
if ($j-$a > $K)
break;
if ($S[$j] < $S[$position])
$position = $j;
}
for ($j = $position; $j > $a; $j--)
{
$temp = $S[$j];
$S[$j] = $S[$j-1];
$S[$j-1] = $temp;
}
$K -= $position-$a;
}
for ($a = 0; $a < $count; $a++){
return($S[$a]);
//echo $S[$a] . " ";
}
}
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($S, $K)
{
$S = str_split($S);
print_r($S);
$count = count($S);
for ($a = 0; $a < $count-1 && $K > 0; $a++)
{
$position = $a;
for ($j = $a+1; $j < $count ; $j++)
{
if ($j-$a > $K)
break;
if ($S[$j] < $S[$position])
$position = $j;
}
for ($j = $position; $j > $a; $j--)
{
$temp = $S[$j];
$S[$j] = $S[$j-1];
$S[$j-1] = $temp;
}
$K -= $position-$a;
}
for ($a = 0; $a < $count; $a++){
echo $S[$a];
return($S[$a]);
//echo $S[$a] . " ";
}
}
Array ( [0] => d [1] => e [2] => c [3] => a [4] => d [5] => e ) a
Array ( [0] => b [1] => b [2] => b [3] => a [4] => b [5] => b [6] => b ) b
Array ( [0] => a [1] => b [2] => r [3] => a [4] => c [5] => a [6] => d [7] => a [8] => b [9] => r [10] => a ) a
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($S, $K)
{
$S = str_split($S);
print_r($S);
$count = count($S);
for ($a = 0; $a < $count-1 && $K > 0; $a++)
{
$position = $a;
for ($j = $a+1; $j < $count ; $j++)
{
if ($j-$a > $K)
break;
if ($S[$j] < $S[$position])
$position = $j;
}
for ($j = $position; $j > $a; $j--)
{
$temp = $S[$j];
$S[$j] = $S[$j-1];
$S[$j-1] = $temp;
}
$K -= $position-$a;
}
for ($a = 0; $a < $count; $a++){
echo $S;
return($S[$a]);
//echo $S[$a] . " ";
}
}
PHP Notice: Array to string conversion in func.php on line 36 Notice: Array to string conversion in func.php on line 36stdout:
Array ( [0] => d [1] => e [2] => c [3] => a [4] => d [5] => e ) Array
PHP Notice: Array to string conversion in func.php on line 36 Notice: Array to string conversion in func.php on line 36stdout:
Array ( [0] => b [1] => b [2] => b [3] => a [4] => b [5] => b [6] => b ) Array
PHP Notice: Array to string conversion in func.php on line 36 Notice: Array to string conversion in func.php on line 36stdout:
Array ( [0] => a [1] => b [2] => r [3] => a [4] => c [5] => a [6] => d [7] => a [8] => b [9] => r [10] => a ) Array
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($S, $K)
{
$S = str_split($S);
print_r($S);
$count = count($S);
for ($a = 0; $a < $count-1 && $K > 0; $a++)
{
$position = $a;
for ($j = $a+1; $j < $count ; $j++)
{
if ($j-$a > $K)
break;
if ($S[$j] < $S[$position])
$position = $j;
}
for ($j = $position; $j > $a; $j--)
{
$temp = $S[$j];
$S[$j] = $S[$j-1];
$S[$j-1] = $temp;
}
$K -= $position-$a;
}
for ($a = 0; $a < $count; $a++){
$S;
return($S[$a]);
//echo $S[$a] . " ";
}
}
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($S, $K)
{
$S = str_split($S);
print_r($S);
$count = count($S);
for ($a = 0; $a < $count-1 && $K > 0; $a++)
{
$position = $a;
for ($j = $a+1; $j < $count ; $j++)
{
if ($j-$a > $K)
break;
if ($S[$j] < $S[$position])
$position = $j;
}
for ($j = $position; $j > $a; $j--)
{
$temp = $S[$j];
$S[$j] = $S[$j-1];
$S[$j-1] = $temp;
}
$K -= $position-$a;
}
for ($a = 0; $a < $count; $a++){
print_r $S;
return($S[$a]);
//echo $S[$a] . " ";
}
}
PHP Parse error: syntax error, unexpected '$S' (T_VARIABLE) in func.php on line 36 Parse error: syntax error, unexpected '$S' (T_VARIABLE) in func.php on line 36
PHP Parse error: syntax error, unexpected '$S' (T_VARIABLE) in func.php on line 36 Parse error: syntax error, unexpected '$S' (T_VARIABLE) in func.php on line 36
PHP Parse error: syntax error, unexpected '$S' (T_VARIABLE) in func.php on line 36 Parse error: syntax error, unexpected '$S' (T_VARIABLE) in func.php on line 36
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($S, $K)
{
$S = str_split($S);
print_r($S);
$count = count($S);
for ($a = 0; $a < $count-1 && $K > 0; $a++)
{
$position = $a;
for ($j = $a+1; $j < $count ; $j++)
{
if ($j-$a > $K)
break;
if ($S[$j] < $S[$position])
$position = $j;
}
for ($j = $position; $j > $a; $j--)
{
$temp = $S[$j];
$S[$j] = $S[$j-1];
$S[$j-1] = $temp;
}
$K -= $position-$a;
}
for ($a = 0; $a < $count; $a++){
print_r ($S);
return($S[$a]);
//echo $S[$a] . " ";
}
}
Array ( [0] => d [1] => e [2] => c [3] => a [4] => d [5] => e ) Array ( [0] => a [1] => d [2] => c [3] => e [4] => d [5] => e )
Array ( [0] => b [1] => b [2] => b [3] => a [4] => b [5] => b [6] => b ) Array ( [0] => b [1] => a [2] => b [3] => b [4] => b [5] => b [6] => b )
Array ( [0] => a [1] => b [2] => r [3] => a [4] => c [5] => a [6] => d [7] => a [8] => b [9] => r [10] => a ) Array ( [0] => a [1] => a [2] => a [3] => a [4] => a [5] => b [6] => r [7] => c [8] => d [9] => b [10] => r )
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($S, $K)
{
$S = str_split($S);
print_r($S);
$count = count($S);
for ($a = 0; $a < $count-1 && $K > 0; $a++)
{
$position = $a;
for ($j = $a+1; $j < $count ; $j++)
{
if ($j-$a > $K)
break;
if ($S[$j] < $S[$position])
$position = $j;
}
for ($j = $position; $j > $a; $j--)
{
$temp = $S[$j];
$S[$j] = $S[$j-1];
$S[$j-1] = $temp;
}
$K -= $position-$a;
}
for ($a = 0; $a < $count; $a++){
print_r ($S);
return($S[$a]);
//echo $S[$a] . " ";
}
}
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($S, $K)
{
$S = str_split($S);
print_r($S);
$count = count($S);
for ($a = 0; $a < $count-1 && $K > 0; $a++)
{
$position = $a;
for ($j = $a+1; $j < $count ; $j++)
{
if ($j-$a > $K)
break;
if ($S[$j] < $S[$position])
$position = $j;
}
for ($j = $position; $j > $a; $j--)
{
$temp = $S[$j];
$S[$j] = $S[$j-1];
$S[$j-1] = $temp;
}
$K -= $position-$a;
}
for ($a = 0; $a < $count; $a++){
print_r ($S[$a]);
return($S[$a]);
//echo $S[$a] . " ";
}
}
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($S, $K)
{
$S = str_split($S);
print_r($S);
$count = count($S);
for ($a = 0; $a < $count-1 && $K > 0; $a++)
{
$position = $a;
for ($j = $a+1; $j < $count ; $j++)
{
if ($j-$a > $K)
break;
if ($S[$j] < $S[$position])
$position = $j;
}
for ($j = $position; $j > $a; $j--)
{
$temp = $S[$j];
$S[$j] = $S[$j-1];
$S[$j-1] = $temp;
}
$K -= $position-$a;
}
for ($a = 0; $a < $count; $a++){
print_r ($S[$a]);
return($S[$a]);
//echo $S[$a] . " ";
}
}
Array ( [0] => d [1] => e [2] => c [3] => a [4] => d [5] => e ) a
Array ( [0] => b [1] => b [2] => b [3] => a [4] => b [5] => b [6] => b ) b
Array ( [0] => a [1] => b [2] => r [3] => a [4] => c [5] => a [6] => d [7] => a [8] => b [9] => r [10] => a ) a
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($S, $K)
{
$S = str_split($S);
print_r($S);
$count = count($S);
for ($a = 0; $a < $count-1 && $K > 0; $a++)
{
$position = $a;
for ($j = $a+1; $j < $count ; $j++)
{
if ($j-$a > $K)
break;
if ($S[$j] < $S[$position])
$position = $j;
}
for ($j = $position; $j > $a; $j--)
{
$temp = $S[$j];
$S[$j] = $S[$j-1];
$S[$j-1] = $temp;
}
$K -= $position-$a;
}
for ($a = 0; $a < $count; $a++){
print_r ($S[$a]);
//echo $S[$a] . " ";
}
}
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($S, $K)
{
$S = str_split($S);
print_r($S);
$count = count($S);
for ($a = 0; $a < $count-1 && $K > 0; $a++)
{
$position = $a;
for ($j = $a+1; $j < $count ; $j++)
{
if ($j-$a > $K)
break;
if ($S[$j] < $S[$position])
$position = $j;
}
for ($j = $position; $j > $a; $j--)
{
$temp = $S[$j];
$S[$j] = $S[$j-1];
$S[$j-1] = $temp;
}
$K -= $position-$a;
}
for ($a = 0; $a < $count; $a++){
print_r ($S[$a]);
//echo $S[$a] . " ";
}
return($S);
}
Invalid result type, string expected (got array)stdout:
Array ( [0] => d [1] => e [2] => c [3] => a [4] => d [5] => e ) adcede
Invalid result type, string expected (got array)stdout:
Array ( [0] => b [1] => b [2] => b [3] => a [4] => b [5] => b [6] => b ) babbbbb
Invalid result type, string expected (got array)stdout:
Array ( [0] => a [1] => b [2] => r [3] => a [4] => c [5] => a [6] => d [7] => a [8] => b [9] => r [10] => a ) aaaaabrcdbr
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($S, $K)
{
$S = str_split($S);
print_r($S);
$count = count($S);
for ($a = 0; $a < $count-1 && $K > 0; $a++)
{
$position = $a;
for ($j = $a+1; $j < $count ; $j++)
{
if ($j-$a > $K)
break;
if ($S[$j] < $S[$position])
$position = $j;
}
for ($j = $position; $j > $a; $j--)
{
$temp = $S[$j];
$S[$j] = $S[$j-1];
$S[$j-1] = $temp;
}
$K -= $position-$a;
}
for ($a = 0; $a < $count; $a++){
//print_r ($S[$a]);
//echo $S[$a] . " ";
}
return($S);
}
Invalid result type, string expected (got array)stdout:
Array ( [0] => d [1] => e [2] => c [3] => a [4] => d [5] => e )
Invalid result type, string expected (got array)stdout:
Array ( [0] => b [1] => b [2] => b [3] => a [4] => b [5] => b [6] => b )
Invalid result type, string expected (got array)stdout:
Array ( [0] => a [1] => b [2] => r [3] => a [4] => c [5] => a [6] => d [7] => a [8] => b [9] => r [10] => a )
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($S, $K)
{
$S = str_split($S);
print_r($S);
$count = count($S);
for ($a = 0; $a < $count-1 && $K > 0; $a++)
{
$position = $a;
for ($j = $a+1; $j < $count ; $j++)
{
if ($j-$a > $K)
break;
if ($S[$j] < $S[$position])
$position = $j;
}
for ($j = $position; $j > $a; $j--)
{
$temp = $S[$j];
$S[$j] = $S[$j-1];
$S[$j-1] = $temp;
}
$K -= $position-$a;
}
for ($a = 0; $a < $count; $a++){
//print_r ($S[$a]);
//echo $S[$a] . " ";
}
return($S);
}
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($S, $K)
{
$S = str_split($S);
print_r($S);
$count = count($S);
for ($a = 0; $a < $count-1 && $K > 0; $a++)
{
$position = $a;
for ($j = $a+1; $j < $count ; $j++)
{
if ($j-$a > $K)
break;
if ($S[$j] < $S[$position])
$position = $j;
}
for ($j = $position; $j > $a; $j--)
{
$temp = $S[$j];
$S[$j] = $S[$j-1];
$S[$j-1] = $temp;
}
$K -= $position-$a;
}
for ($a = 0; $a < $count; $a++){
//print_r ($S[$a]);
//echo $S[$a] . " ";
}
$S2 = implode(", ", $S);
return($S);
}
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($S, $K)
{
$S = str_split($S);
print_r($S);
$count = count($S);
for ($a = 0; $a < $count-1 && $K > 0; $a++)
{
$position = $a;
for ($j = $a+1; $j < $count ; $j++)
{
if ($j-$a > $K)
break;
if ($S[$j] < $S[$position])
$position = $j;
}
for ($j = $position; $j > $a; $j--)
{
$temp = $S[$j];
$S[$j] = $S[$j-1];
$S[$j-1] = $temp;
}
$K -= $position-$a;
}
for ($a = 0; $a < $count; $a++){
//print_r ($S[$a]);
//echo $S[$a] . " ";
}
$S2 = implode(", ", $S);
print $S2;
//return($S);
}
Invalid result type, string expected (got NULL)stdout:
Array ( [0] => d [1] => e [2] => c [3] => a [4] => d [5] => e ) a, d, c, e, d, e
Invalid result type, string expected (got NULL)stdout:
Array ( [0] => b [1] => b [2] => b [3] => a [4] => b [5] => b [6] => b ) b, a, b, b, b, b, b
Invalid result type, string expected (got NULL)stdout:
Array ( [0] => a [1] => b [2] => r [3] => a [4] => c [5] => a [6] => d [7] => a [8] => b [9] => r [10] => a ) a, a, a, a, a, b, r, c, d, b, r
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($S, $K)
{
$S = str_split($S);
print_r($S);
$count = count($S);
for ($a = 0; $a < $count-1 && $K > 0; $a++)
{
$position = $a;
for ($j = $a+1; $j < $count ; $j++)
{
if ($j-$a > $K)
break;
if ($S[$j] < $S[$position])
$position = $j;
}
for ($j = $position; $j > $a; $j--)
{
$temp = $S[$j];
$S[$j] = $S[$j-1];
$S[$j-1] = $temp;
}
$K -= $position-$a;
}
for ($a = 0; $a < $count; $a++){
//print_r ($S[$a]);
//echo $S[$a] . " ";
}
$S2 = implode("", $S);
print $S2;
//return($S);
}
Invalid result type, string expected (got NULL)stdout:
Array ( [0] => d [1] => e [2] => c [3] => a [4] => d [5] => e ) adcede
Invalid result type, string expected (got NULL)stdout:
Array ( [0] => b [1] => b [2] => b [3] => a [4] => b [5] => b [6] => b ) babbbbb
Invalid result type, string expected (got NULL)stdout:
Array ( [0] => a [1] => b [2] => r [3] => a [4] => c [5] => a [6] => d [7] => a [8] => b [9] => r [10] => a ) aaaaabrcdbr
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($S, $K)
{
$S = str_split($S);
print_r($S);
$count = count($S);
for ($a = 0; $a < $count-1 && $K > 0; $a++)
{
$position = $a;
for ($j = $a+1; $j < $count ; $j++)
{
if ($j-$a > $K)
break;
if ($S[$j] < $S[$position])
$position = $j;
}
for ($j = $position; $j > $a; $j--)
{
$temp = $S[$j];
$S[$j] = $S[$j-1];
$S[$j-1] = $temp;
}
$K -= $position-$a;
}
for ($a = 0; $a < $count; $a++){
//print_r ($S[$a]);
//echo $S[$a] . " ";
}
$S2 = implode("", $S);
print $S2;
/return($S);
}
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($S, $K)
{
$S = str_split($S);
print_r($S);
$count = count($S);
for ($a = 0; $a < $count-1 && $K > 0; $a++)
{
$position = $a;
for ($j = $a+1; $j < $count ; $j++)
{
if ($j-$a > $K)
break;
if ($S[$j] < $S[$position])
$position = $j;
}
for ($j = $position; $j > $a; $j--)
{
$temp = $S[$j];
$S[$j] = $S[$j-1];
$S[$j-1] = $temp;
}
$K -= $position-$a;
}
for ($a = 0; $a < $count; $a++){
//print_r ($S[$a]);
//echo $S[$a] . " ";
}
$S2 = implode("", $S);
print $S2;
return($S2);
}
Array ( [0] => d [1] => e [2] => c [3] => a [4] => d [5] => e ) adcede
Array ( [0] => b [1] => b [2] => b [3] => a [4] => b [5] => b [6] => b ) babbbbb
Array ( [0] => a [1] => b [2] => r [3] => a [4] => c [5] => a [6] => d [7] => a [8] => b [9] => r [10] => a ) aaaaabrcdbr
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($S, $K)
{
$S = str_split($S);
print_r($S);
$count = count($S);
for ($a = 0; $a < $count-1 && $K > 0; $a++)
{
$position = $a;
for ($j = $a+1; $j < $count ; $j++)
{
if ($j-$a > $K)
break;
if ($S[$j] < $S[$position])
$position = $j;
}
for ($j = $position; $j > $a; $j--)
{
$temp = $S[$j];
$S[$j] = $S[$j-1];
$S[$j-1] = $temp;
}
$K -= $position-$a;
}
for ($a = 0; $a < $count; $a++){
//print_r ($S[$a]);
//echo $S[$a] . " ";
}
$S2 = implode("", $S);
//print $S2;
return($S2);
}
Array ( [0] => d [1] => e [2] => c [3] => a [4] => d [5] => e )
Array ( [0] => b [1] => b [2] => b [3] => a [4] => b [5] => b [6] => b )
Array ( [0] => a [1] => b [2] => r [3] => a [4] => c [5] => a [6] => d [7] => a [8] => b [9] => r [10] => a )
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($S, $K)
{
$S = str_split($S);
// print_r($S);
$count = count($S);
for ($a = 0; $a < $count-1 && $K > 0; $a++)
{
$position = $a;
for ($j = $a+1; $j < $count ; $j++)
{
if ($j-$a > $K)
break;
if ($S[$j] < $S[$position])
$position = $j;
}
for ($j = $position; $j > $a; $j--)
{
$temp = $S[$j];
$S[$j] = $S[$j-1];
$S[$j-1] = $temp;
}
$K -= $position-$a;
}
for ($a = 0; $a < $count; $a++){
//print_r ($S[$a]);
//echo $S[$a] . " ";
}
$S2 = implode("", $S);
//print $S2;
return($S2);
}
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($S, $K)
{
$S = str_split($S);
$count = count($S);
for ($a = 0; $a < $count-1 && $K > 0; $a++)
{
$position = $a;
for ($j = $a+1; $j < $count ; $j++)
{
if ($j-$a > $K)
break;
if ($S[$j] < $S[$position])
$position = $j;
}
for ($j = $position; $j > $a; $j--)
{
$temp = $S[$j];
$S[$j] = $S[$j-1];
$S[$j-1] = $temp;
}
$K -= $position-$a;
}
for ($a = 0; $a < $count; $a++){
//print_r ($S[$a]);
//echo $S[$a] . " ";
}
$S2 = implode("", $S);
//print $S2;
return($S2);
}
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($S, $K)
{
$S = str_split($S);
$count = count($S);
for ($a = 0; $a < $count-1 && $K > 0; $a++)
{
$position = $a;
for ($j = $a+1; $j < $count ; $j++)
{
if ($j-$a > $K)
break;
if ($S[$j] < $S[$position])
$position = $j;
}
for ($j = $position; $j > $a; $j--)
{
$temp = $S[$j];
$S[$j] = $S[$j-1];
$S[$j-1] = $temp;
}
$K -= $position-$a;
}
for ($a = 0; $a < $count; $a++){
//print_r ($S[$a]);
}
$S2 = implode("", $S);
return($S2);
}
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($S, $K)
{
$S = str_split($S);
$count = count($S);
for ($a = 0; $a < $count-1 && $K > 0; $a++)
{
$position = $a;
for ($j = $a+1; $j < $count ; $j++)
{
if ($j-$a > $K)
break;
if ($S[$j] < $S[$position])
$position = $j;
}
for ($j = $position; $j > $a; $j--)
{
$temp = $S[$j];
$S[$j] = $S[$j-1];
$S[$j-1] = $temp;
}
$K -= $position-$a;
}
// for ($a = 0; $a < $count; $a++){
// }
$S2 = implode("", $S);
return($S2);
}
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($S, $K)
{
$S = str_split($S);
$count = count($S);
for ($a = 0; $a < $count-1 && $K > 0; $a++)
{
$position = $a;
for ($j = $a+1; $j < $count ; $j++)
{
if ($j-$a > $K)
break;
if ($S[$j] < $S[$position])
$position = $j;
}
for ($j = $position; $j > $a; $j--)
{
$temp = $S[$j];
$S[$j] = $S[$j-1];
$S[$j-1] = $temp;
}
$K -= $position-$a;
}
$S2 = implode("", $S);
return($S2);
}
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($S, $K)
{
$S = str_split($S);
$count = count($S);
for ($initial = 0; $initial < $count-1 && $K > 0; $initial++)
{
$position = $initial;
for ($index = $initial+1; $index < $count ; $index++)
{
if ($index-$initial > $K)
break;
if ($S[$index] < $S[$position])
$position = $index;
}
for ($index = $position; $index > $initial; $index--)
{
$temp = $S[$index];
$S[$index] = $S[$index-1];
$S[$index-1] = $temp;
}
$K -= $position-$initial;
}
$S2 = implode("", $S);
return($S2);
}
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($S, $K)
{
$S = str_split($S);
$count = count($S);
for ($initial = 0; $initial < $count-1 && $K > 0; $initial++)
{
$position = $initial;
for ($index = $initial+1; $index < $count ; $index++)
{
if ($index-$initial > $K)
break;
if ($S[$index] < $S[$position])
$position = $index;
}
for ($index = $position; $index > $initial; $index--)
{
$temp = $S[$index];
$S[$index] = $S[$index-1];
$S[$index-1] = $temp;
}
$K -= $position-$initial;
}
$S2 = implode("", $S);
return($S2);
}
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($S, $K)
{
$S = str_split($S);
$count = count($S);
for ($initial = 0; $initial < $count-1 && $K > 0; $initial++)
{
$position = $initial;
for ($index = $initial+1; $index < $count ; $index++)
{
if ($index-$initial > $K)
break;
if ($S[$index] < $S[$position])
$position = $index;
}
for ($index = $position; $index > $initial; $index--)
{
$temp = $S[$index];
$S[$index] = $S[$index-1];
$S[$index-1] = $temp;
}
$K -= $position-$initial;
}
$S2 = implode("", $S);
return($S2);
}
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($S, $K)
{
$S = str_split($S);
$count = count($S);
for ($initial = 0; $initial < $count-1 && $K > 0; $initial++)
{
$position = $initial;
for ($index = $initial+1; $index < $count ; $index++)
{
if ($index-$initial > $K)
break;
if ($S[$index] < $S[$position])
$position = $index;
}
for ($index = $position; $index > $initial; $index--)
{
$temp = $S[$index];
$S[$index] = $S[$index-1];
$S[$index-1] = $temp;
}
$K -= $position-$initial;
}
$S2 = implode("", $S);
return($S2);
}
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($input, $K)
{
$input = str_split($input);
$count = count($input);
for ($initial = 0; $initial < $count-1 && $K > 0; $initial++)
{
$position = $initial;
for ($index = $initial+1; $index < $count ; $index++)
{
if ($index-$initial > $K)
break;
if ($input[$index] < $input[$position])
$position = $index;
}
for ($index = $position; $index > $initial; $index--)
{
$temp = $input[$index];
$input[$index] = $input[$index-1];
$input[$index-1] = $temp;
}
$K -= $position-$initial;
}
$input2 = implode("", $input);
return($input2);
}
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($S, $K)
{
$S = str_split($S);
$count = count($S);
for ($initial = 0; $initial < $count-1 && $K > 0; $initial++)
{
$position = $initial;
for ($index = $initial+1; $index < $count ; $index++)
{
if ($index-$initial > $K)
break;
if ($S[$index] < $S[$position])
$position = $index;
}
for ($index = $position; $index > $initial; $index--)
{
$temp = $S[$index];
$S[$index] = $S[$index-1];
$S[$index-1] = $temp;
}
$K -= $position-$initial;
}
$S2 = implode("", $S);
return($S2);
}
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($S, $K)
{
$input = str_split($S);
$count = count($input);
for ($initial = 0; $initial < $count-1 && $K > 0; $initial++)
{
$position = $initial;
for ($index = $initial+1; $index < $count ; $index++)
{
if ($index-$initial > $K)
break;
if ($input[$index] < $input[$position])
$position = $index;
}
for ($index = $position; $index > $initial; $index--)
{
$temp = $input[$index];
$input[$index] = $input[$index-1];
$input[$index-1] = $temp;
}
$K -= $position-$initial;
}
$input2 = implode("", $input);
return($input2);
}
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($S, $K)
{
$input = str_split($S);
$count = count($input);
for ($initial = 0; $initial < $count-1 && $K > 0; $initial++)
{
$position = $initial;
for ($index = $initial+1; $index < $count ; $index++)
{
if ($index-$initial > $K)
break;
if ($input[$index] < $input[$position])
$position = $index;
}
for ($index = $position; $index > $initial; $index--)
{
$temp = $input[$index];
$input[$index] = $input[$index-1];
$input[$index-1] = $temp;
}
$K -= $position-$initial;
}
$result = implode("", $input);
return($result);
}
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($S, $K)
{
$input = str_split($S);
$count = count($input);
for ($initial = 0; $initial < $count-1 && $K > 0; $initial++)
{
$position = $initial;
for ($index = $initial+1; $index < $count ; $index++)
{
if ($index-$initial > $K)
break;
if ($input[$index] < $input[$position])
$position = $index;
}
for ($index = $position; $index > $initial; $index--)
{
$temp = $input[$index];
$input[$index] = $input[$index-1];
$input[$index-1] = $temp;
}
$K -= $position-$initial;
}
$result = implode("", $input);
return($result);
}
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($S, $K)
{
$input = str_split($S);
$count = count($input);
for ($initial = 0; $initial < $count-1 && $K > 0; $initial++)
{
$position = $initial;
for ($index = $initial+1; $index < $count ; $index++)
{
if ($index-$initial > $K)
break;
if ($input[$index] < $input[$position])
$position = $index;
}
for ($index = $position; $index > $initial; $index--)
{
$temp = $input[$index];
$input[$index] = $input[$index-1];
$input[$index-1] = $temp;
}
$K -= $position-$initial;
}
$result = implode("", $input);
return($result);
}
// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";
function solution($S, $K)
{
$input = str_split($S);
$count = count($input);
for ($initial = 0; $initial < $count-1 && $K > 0; $initial++)
{
$position = $initial;
for ($index = $initial+1; $index < $count ; $index++)
{
if ($index-$initial > $K)
break;
if ($input[$index] < $input[$position])
$position = $index;
}
for ($index = $position; $index > $initial; $index--)
{
$temp = $input[$index];
$input[$index] = $input[$index-1];
$input[$index-1] = $temp;
}
$K -= $position-$initial;
}
$result = implode("", $input);
return($result);
}
The following issues have been detected: timeout errors.
String sorted almost to the end, N = 80,000, K = 50,000.
Killed. Hard limit reached: 8.000 sec.
Most of the string consists of letters sorted in descending order, N = 80,000.
Killed. Hard limit reached: 8.000 sec.
Single repeating letter plus a short suffix.
Killed. Hard limit reached: 8.000 sec.