{"id":27099,"date":"2018-01-05T20:46:58","date_gmt":"2018-01-05T15:16:58","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=27099"},"modified":"2018-01-05T20:46:58","modified_gmt":"2018-01-05T15:16:58","slug":"c-programming-lexicographically-minimum-string-rotation","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/c-programming-lexicographically-minimum-string-rotation\/","title":{"rendered":"C++ Programming &#8211; Lexicographically minimum string rotation"},"content":{"rendered":"<p>Write code to find lexicographic minimum in a circular array, e.g. for the array BCABDADAB, the lexicographic minimum is ABBCABDAD.<span id=\"more-142553\"><\/span><\/p>\n<p>Source: Google Written Test<\/p>\n<p>More Examples:<\/p>\n<pre>Input:  GEEKSQUIZ\r\nOutput: EEKSQUIZG\r\n\r\nInput:  GFG\r\nOutput: FGG\r\n\r\nInput:  GEEKSFORGEEKS\r\nOutput: EEKSFORGEEKSG<\/pre>\n<p>Following is a simple solution. Let the given string be \u2018str\u2019<br \/>\n1) Concatenate \u2018str\u2019 with itself and store in a temporary string say \u2018concat\u2019.<br \/>\n2) Create an array of strings to store all rotations of \u2018str\u2019. Let the array be \u2018arr\u2019.<br \/>\n3) Find all rotations of \u2018str\u2019 by taking substrings of \u2018concat\u2019 at index 0, 1, 2..n-1. Store these rotations in arr[]\n4) Sort arr[] and return arr[0].<\/p>\n[ad type=&#8221;banner&#8221;]\n<p>Following is C++ implementation of above solution.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">C++ Program<\/span> <\/div> <pre class=\"language-cpp code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-cpp code-embed-code\">\/\/ A simple C++ program to find lexicographically minimum rotation<br\/>\/\/ of a given string<br\/>#include &lt;iostream&gt;<br\/>#include &lt;algorithm&gt;<br\/>using namespace std;<br\/> <br\/>\/\/ This functionr return lexicographically minimum<br\/>\/\/ rotation of str<br\/>string minLexRotation(string str)<br\/>{<br\/>    \/\/ Find length of given string<br\/>    int n = str.length();<br\/> <br\/>    \/\/ Create an array of strings to store all rotations<br\/>    string arr[n];<br\/> <br\/>    \/\/ Create a concatenation of string with itself<br\/>    string concat = str + str;<br\/> <br\/>    \/\/ One by one store all rotations of str in array.<br\/>    \/\/ A rotation is obtained by getting a substring of concat<br\/>    for (int i = 0; i &lt; n; i++)<br\/>        arr[i] = concat.substr(i, n);<br\/> <br\/>    \/\/ Sort all rotations<br\/>    sort(arr, arr+n);<br\/> <br\/>    \/\/ Return the first rotation from the sorted array<br\/>    return arr[0];<br\/>}<br\/> <br\/>\/\/ Driver program to test above function<br\/>int main()<br\/>{<br\/>    cout &lt;&lt; minLexRotation(&quot;GEEKSFORGEEKS&quot;) &lt;&lt; endl;<br\/>    cout &lt;&lt; minLexRotation(&quot;GEEKSQUIZ&quot;) &lt;&lt; endl;<br\/>    cout &lt;&lt; minLexRotation(&quot;BCABDADAB&quot;) &lt;&lt; endl;<br\/>}<\/code><\/pre> <\/div>\n<p>Output:<\/p>\n<pre>EEKSFORGEEKSG\r\nEEKSQUIZG\r\nABBCABDAD<\/pre>\n<p>Time complexity of the above solution is O(n<sup>2<\/sup>Logn) under the assumption that we have used a O(nLogn) sorting algorithm.<\/p>\n<p>This problem can be solved using more efficient methods like <a href=\"http:\/\/en.wikipedia.org\/wiki\/Lexicographically_minimal_string_rotation#Booth.27s_Algorithm\" target=\"_blank\" rel=\"noopener noreferrer\">Booth\u2019s Algorithm<\/a> which solves the problem in O(n) time. We will soon be covering these methods as separate posts.<\/p>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>Write code to find lexicographic minimum in a circular array, e.g. for the array BCABDADAB, the lexicographic minimum is ABBCABDAD.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69969,83515,1],"tags":[80802,73394,73654,75696,75706,80812,73634,80808,80803,75705,73676,70384,73652,80806,80809,80807,72138,71159,73184,73636,73653,80810],"class_list":["post-27099","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-c-programming-3","category-coding","tag-c-words","tag-compare-strings-in-c","tag-competitive-programming-3-pdf","tag-dictionary-order","tag-dictionary-program-in-c","tag-enumeration-grammar","tag-example-of-suffix","tag-good-string","tag-hackerrank-test","tag-ordered-dictionary","tag-permutation","tag-permutation-program-in-c","tag-prefixes-and-suffixes-pdf","tag-programming-dictionary","tag-radix-64","tag-smallest-string","tag-sort-dictionary","tag-sorting-program","tag-string-algorithms","tag-suffix","tag-suffix-of-or","tag-universal-cycles"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27099","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=27099"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27099\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=27099"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=27099"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=27099"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}