{"id":25466,"date":"2017-10-15T18:47:14","date_gmt":"2017-10-15T13:17:14","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=25466"},"modified":"2017-10-15T18:47:14","modified_gmt":"2017-10-15T13:17:14","slug":"java-programming-print-possible-strings-can-made-placing-spaces","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/java-programming-print-possible-strings-can-made-placing-spaces\/","title":{"rendered":"JAVA Programming-Print all possible strings that can be made by placing spaces"},"content":{"rendered":"<p>Given a string you need to print all possible strings that can be made by placing spaces (zero or one) in between them.<\/p>\n<pre>Input:  str[] = \"ABC\"\r\nOutput: ABC\r\n        AB C\r\n        A BC\r\n        A B C<\/pre>\n<p>The idea is to use recursion and create a buffer that one by one contains all output strings having spaces. We keep updating buffer in every recursive call. If the length of given string is \u2018n\u2019 our updated string can have maximum length of n + (n-1) i.e. 2n-1. So we create buffer size of 2n (one extra character for string termination).<br \/>\nWe leave 1st character as it is, starting from the 2nd character, we can either fill a space or a character. Thus one can write a recursive function like below.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">JAVA<\/span> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\"># Python program to print permutations of a given string with<br\/># spaces.<br\/> <br\/># Utility function<br\/>def toString(List):<br\/>    s = &quot;&quot;<br\/>    for x in List:<br\/>        if x == &#039;\\0&#039;:<br\/>            break<br\/>        s += x<br\/>    return s<br\/> <br\/># Function recursively prints the strings having space pattern.<br\/># i and j are indices in &#039;str[]&#039; and &#039;buff[]&#039; respectively<br\/>def printPatternUtil(string, buff, i, j, n):<br\/>    if i == n:<br\/>        buff[j] = &#039;\\0&#039;<br\/>        print toString(buff)<br\/>        return<br\/> <br\/>    # Either put the character<br\/>    buff[j] = string[i]<br\/>    printPatternUtil(string, buff, i+1, j+1, n)<br\/> <br\/>    # Or put a space followed by next character<br\/>    buff[j] = &#039; &#039;<br\/>    buff[j+1] = string[i]<br\/> <br\/>    printPatternUtil(string, buff, i+1, j+2, n)<br\/> <br\/># This function creates buf[] to store individual output string<br\/># and uses printPatternUtil() to print all permutations.<br\/>def printPattern(string):<br\/>    n = len(string)<br\/> <br\/>    # Buffer to hold the string containing spaces<br\/>    buff = [0] * (2*n) # 2n-1 characters and 1 string terminator<br\/> <br\/>    # Copy the first character as it is, since it will be always<br\/>    # at first position<br\/>    buff[0] = string[0]<br\/> <br\/>    printPatternUtil(string, buff, 1, 1, n)<br\/> <br\/># Driver program<br\/>string = &quot;ABCD&quot;<br\/>printPattern(string)<br\/> <\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>JAVA Programming-Print all possible strings that can be made by placing spaces &#8211; Searching and Sorting &#8211; The idea is to use recursion and create a buffer.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69969,1,2139,71670],"tags":[73466,73421,73469,73434,73438,73442,73452,73470,73463,73454,73464,73455,73445,73468,73456,73453,73450,73423,73436,73437,73431,73440,73429,73457,73461,73467,73444,73462,73432,73459,73422,73430,73433,73426,73425,73447,73448,73424,73446,73428,73465,73449,73441,73458,73439,73443,73427,73435,73460,73451],"class_list":["post-25466","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-coding","category-java","category-searching-and-sorting","tag-c-printf","tag-c-programming-print-all-possible-strings-that-can-be-made-by-placing-spaces","tag-c-sprintf","tag-character-java","tag-format-java","tag-format-printf","tag-format-specifier-java","tag-format-specifiers-in-c","tag-how-to-take-input-from-user-in-java","tag-java-flag","tag-java-format","tag-java-format-print","tag-java-format-specifiers","tag-java-format-string","tag-java-print","tag-java-print-format","tag-java-print-line","tag-java-printf","tag-java-printf-double","tag-java-printf-example","tag-java-printf-format","tag-java-printf-string","tag-java-sprintf","tag-java-stdout","tag-java-string-format","tag-java-string-format-example","tag-java-string-printf","tag-java-system-out-format","tag-java-system-out-printf","tag-print-java","tag-printf","tag-printf-c","tag-printf-example","tag-printf-float","tag-printf-format","tag-printf-format-java","tag-printf-in-c","tag-printf-java","tag-printf-options","tag-printf-string","tag-string-format-java","tag-string-java","tag-string-printf","tag-string-programs-in-java","tag-system-out-format","tag-system-out-format-java","tag-system-out-printf","tag-system-out-printf-java","tag-system-printf","tag-using-printf"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25466","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\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=25466"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25466\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=25466"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=25466"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=25466"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}