Today I have solved the problem in javascript
You are given a string of length N and a parameter k. The string can be manipulated by taking one of the first k letters and moving it to the end.
Write a program to determine the lexicographically smallest string that can be created after an unlimited number of moves.
For example, suppose we are given the string daily and k = 1. The best we can create in this case is ailyd.
const inputString = "daily";
const kValue = 1;
const result = getLexicographicallySmallestString(inputString, kValue);
function getLexicographicallySmallestString(s, k) {
let smallestString = s;
for (let i = 1; i <= k; i++) {
let rotatedString = s.substring(i) + s.substring(0, i);
if (rotatedString < smallestString) {
smallestString = rotatedString;
} return smallestString;
}console.log(result);
Output:
ailyd
Comments
Post a Comment