2023-04-27 12:50:36 -05:00

178 lines
12 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Combinations.java</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
<!--
body {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
table {color: #888888; background-color: #313335; font-family: monospace; font-weight: bold}
.literal {color: #cc7832}
.ST0 {color: #287bde}
.number {color: #6897bb}
.string {color: #6a8759}
.ST2 {color: #9876aa}
.ST6 {color: #ffc66d}
.ST5 {color: #8a653b}
.ST1 {color: #ffc66d; font-family: monospace; font-weight: bold; font-style: italic}
.ST7 {color: #9876aa; font-family: monospace; font-weight: bold; font-style: italic}
.ST3 {font-family: monospace; font-weight: bold; font-style: italic}
.ST4 {color: #808080; font-family: monospace; font-weight: bold; font-style: italic}
.comment {color: #808080}
.whitespace {color: #505050}
-->
</style>
</head>
<body>
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 2/Assignments/MP5_CalebFontenot/src/main/java/com/calebfontenot/mp5_calebfontenot/Combinations.java</td></tr></table>
<pre>
<span class="comment">/*</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt</span><span class="comment"> to change this license</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java</span><span class="comment"> to edit this template</span>
<span class="comment"> */</span>
<span class="literal">package</span> com.calebfontenot.mp5_calebfontenot;
<span class="comment">/**</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@author</span> <span class="comment">caleb</span>
<span class="comment">*/</span>
<span class="literal">import</span> java.util.ArrayList;
<span class="literal">import</span> java.util.Collections;
<span class="literal">import</span> java.util.Comparator;
<span class="literal">import</span> java.util.HashSet;
<span class="literal">import</span> java.util.Iterator;
<span class="literal">import</span> java.util.List;
<span class="literal">import</span> java.util.Set;
<span class="literal">class</span> Combinations
{
<span class="literal">private</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST1">findCombinations</span>(String[] A, <span class="literal">int</span> i, <span class="literal">int</span> k,
Set&lt;List&lt;String&gt;&gt; subarrays,
List&lt;String&gt; out)
{
<span class="literal">if</span> (A.<span class="ST2">length</span> == <span class="number">0</span> || k &gt; A.<span class="ST2">length</span>)
{
<span class="literal">return</span>;
}
<span class="comment">// base case: combination size is `k`</span>
<span class="literal">if</span> (k == <span class="number">0</span>)
{
subarrays.add(<span class="literal">new</span> ArrayList&lt;&gt;(out));
<span class="literal">return</span>;
}
<span class="comment">// start from the next index till the last index</span>
<span class="literal">for</span> (<span class="literal">int</span> j = i; j &lt; A.<span class="ST2">length</span>; j++)
{
<span class="comment">// add current element `A[j]` to the solution and recur for next index</span>
<span class="comment">// `j+1` with one less element `k-1`</span>
out.add(A[j]);
<span class="ST3">findCombinations</span>(A, j + <span class="number">1</span>, k - <span class="number">1</span>, subarrays, out);
out.remove(out.size() - <span class="number">1</span>); <span class="comment">// backtrack</span>
}
}
<span class="literal">private</span> <span class="literal">static</span> Set&lt;List&lt;String&gt;&gt; <span class="ST4">findCombinations</span>(String[] A, <span class="literal">int</span> k)
{
Set&lt;List&lt;String&gt;&gt; subarrays = <span class="literal">new</span> HashSet&lt;&gt;();
<span class="ST3">findCombinations</span>(A, <span class="number">0</span>, k, subarrays, <span class="literal">new</span> ArrayList&lt;&gt;());
<span class="literal">return</span> subarrays;
}
<span class="literal">private</span> <span class="literal">static</span> Set&lt;List&lt;String&gt;&gt; <span class="ST1">findAllCombinations</span>(String[] A)
{
Set&lt;List&lt;String&gt;&gt; subarrays = <span class="literal">new</span> HashSet&lt;&gt;();
<span class="literal">for</span> (<span class="literal">int</span> k = <span class="number">1</span>; k &lt;= A.<span class="ST2">length</span>; ++k)
{
<span class="ST3">findCombinations</span>(A, <span class="number">0</span>, k, subarrays, <span class="literal">new</span> ArrayList&lt;&gt;());
}
<span class="literal">return</span> subarrays;
}
<span class="comment">/**</span> <span class="comment">Finds</span> <span class="comment">all</span> <span class="comment">distinct</span> <span class="comment">combinations</span> <span class="comment">of</span> <span class="comment">all</span> <span class="comment">sizes</span> <span class="comment">for</span> <span class="comment">elements</span> <span class="comment">of</span> <span class="comment">array</span><span class="comment">.</span>
<span class="comment"> * </span>
<span class="comment"> * </span><span class="comment">@param</span> <span class="ST5">A</span> <span class="comment">the</span> <span class="comment">elements</span> <span class="comment">to</span> <span class="comment">find</span> <span class="comment">their</span> <span class="comment">combinations</span>
<span class="comment"> * </span><span class="comment">@return</span> <span class="comment">all</span> <span class="comment">distinct</span> <span class="comment">combinations</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">elements</span><span class="comment">, </span><span class="comment">sorted</span> <span class="comment">by</span> <span class="comment">length</span><span class="comment">.</span> <span class="comment">ascending</span> <span class="comment">order</span><span class="comment">.</span>
<span class="comment">*/</span>
<span class="literal">public</span> <span class="literal">static</span> ArrayList&lt;String&gt; <span class="ST1">allCombinations</span>(String[] A)
{
Set&lt;List&lt;String&gt;&gt; set = <span class="ST3">findAllCombinations</span>(A);
ArrayList&lt;String&gt; all = <span class="literal">new</span> ArrayList&lt;String&gt;();
Iterator it = set.iterator();
<span class="literal">while</span> (it.hasNext())
{
List&lt;String&gt; list = (List&lt;String&gt;) it.next();
String s1 = <span class="string">&quot;&quot;</span>;
<span class="literal">for</span> (String s2 : list)
{
s1 += s2;
}
all.add(s1);
}
Collections.<span class="ST3">sort</span>(all, <span class="literal">new</span> Comparator&lt;String&gt;(){
@Override
<span class="literal">public</span> <span class="literal">int</span> <span class="ST6">compare</span>(String o1, String o2)
{
<span class="literal">return</span> o1.length() - o2.length();
}
});
<span class="literal">return</span> all;
}
<span class="comment">/**</span> <span class="comment">Finds</span> <span class="comment">all</span> <span class="comment">distinct</span> <span class="comment">combinations</span> <span class="comment">of</span> <span class="comment">all</span> <span class="comment">sizes</span> <span class="comment">for</span> <span class="comment">chars</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">String</span><span class="comment">.</span>
<span class="comment"> * </span>
<span class="comment"> * </span><span class="comment">@param</span> <span class="ST5">A</span> <span class="comment">the</span> <span class="comment">characters</span> <span class="comment">to</span> <span class="comment">find</span> <span class="comment">their</span> <span class="comment">combinations</span><span class="comment">.</span>
<span class="comment"> * </span><span class="comment">@return</span> <span class="comment">all</span> <span class="comment">distinct</span> <span class="comment">combinations</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">characters</span> <span class="comment">sorted</span> <span class="comment">by</span> <span class="comment">length</span><span class="comment">, </span><span class="comment">ascending</span> <span class="comment">order</span><span class="comment">.</span>
<span class="comment">*/</span>
<span class="literal">public</span> <span class="literal">static</span> ArrayList&lt;String&gt; <span class="ST1">allCombinations</span>(String a)
{
String[] A = <span class="literal">new</span> String[a.length()];
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; A.<span class="ST2">length</span>; ++i)
{
A[i] = Character.<span class="ST3">toString</span>(a.charAt(i));
}
Set&lt;List&lt;String&gt;&gt; set = <span class="ST3">findAllCombinations</span>(A);
ArrayList&lt;String&gt; all = <span class="literal">new</span> ArrayList&lt;String&gt;();
Iterator it = set.iterator();
<span class="literal">while</span> (it.hasNext())
{
List&lt;String&gt; list = (List&lt;String&gt;) it.next();
String s1 = <span class="string">&quot;&quot;</span>;
<span class="literal">for</span> (String s2 : list)
{
s1 += s2;
}
all.add(s1);
}
Collections.<span class="ST3">sort</span>(all, <span class="literal">new</span> Comparator&lt;String&gt;(){
@Override
<span class="literal">public</span> <span class="literal">int</span> <span class="ST6">compare</span>(String o1, String o2)
{
<span class="literal">return</span> o1.length() - o2.length();
}
});
<span class="literal">return</span> all;
}
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST1">main</span>(String[] args)
{
String[] A =
{
<span class="string">&quot;</span><span class="string">1</span><span class="string">&quot;</span>, <span class="string">&quot;</span><span class="string">2</span><span class="string">&quot;</span>, <span class="string">&quot;</span><span class="string">3</span><span class="string">&quot;</span>, <span class="string">&quot;</span><span class="string">4</span><span class="string">&quot;</span>
};
<span class="literal">int</span> <span class="comment">k</span> = <span class="number">2</span>;
<span class="comment">// process elements from left to right</span>
System.<span class="ST7">out</span>.println(<span class="ST3">a</span><span class="ST3">llCombinations</span>(A));
System.<span class="ST7">out</span>.println(<span class="ST3">a</span><span class="ST3">llCombinations</span>(<span class="string">&quot;</span><span class="string">1234</span><span class="string">&quot;</span>));
}
}
</pre></body>
</html>