178 lines
12 KiB
HTML
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<List<String>> subarrays,
|
|
List<String> out)
|
|
{
|
|
<span class="literal">if</span> (A.<span class="ST2">length</span> == <span class="number">0</span> || k > 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<>(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 < 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<List<String>> <span class="ST4">findCombinations</span>(String[] A, <span class="literal">int</span> k)
|
|
{
|
|
Set<List<String>> subarrays = <span class="literal">new</span> HashSet<>();
|
|
<span class="ST3">findCombinations</span>(A, <span class="number">0</span>, k, subarrays, <span class="literal">new</span> ArrayList<>());
|
|
<span class="literal">return</span> subarrays;
|
|
}
|
|
|
|
<span class="literal">private</span> <span class="literal">static</span> Set<List<String>> <span class="ST1">findAllCombinations</span>(String[] A)
|
|
{
|
|
Set<List<String>> subarrays = <span class="literal">new</span> HashSet<>();
|
|
<span class="literal">for</span> (<span class="literal">int</span> k = <span class="number">1</span>; k <= 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<>());
|
|
}
|
|
<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<String> <span class="ST1">allCombinations</span>(String[] A)
|
|
{
|
|
Set<List<String>> set = <span class="ST3">findAllCombinations</span>(A);
|
|
ArrayList<String> all = <span class="literal">new</span> ArrayList<String>();
|
|
Iterator it = set.iterator();
|
|
<span class="literal">while</span> (it.hasNext())
|
|
{
|
|
List<String> list = (List<String>) it.next();
|
|
String s1 = <span class="string">""</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<String>(){
|
|
@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<String> <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 < A.<span class="ST2">length</span>; ++i)
|
|
{
|
|
A[i] = Character.<span class="ST3">toString</span>(a.charAt(i));
|
|
}
|
|
Set<List<String>> set = <span class="ST3">findAllCombinations</span>(A);
|
|
ArrayList<String> all = <span class="literal">new</span> ArrayList<String>();
|
|
Iterator it = set.iterator();
|
|
<span class="literal">while</span> (it.hasNext())
|
|
{
|
|
List<String> list = (List<String>) it.next();
|
|
String s1 = <span class="string">""</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<String>(){
|
|
@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">"</span><span class="string">1</span><span class="string">"</span>, <span class="string">"</span><span class="string">2</span><span class="string">"</span>, <span class="string">"</span><span class="string">3</span><span class="string">"</span>, <span class="string">"</span><span class="string">4</span><span class="string">"</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">"</span><span class="string">1234</span><span class="string">"</span>));
|
|
|
|
}
|
|
}
|
|
|
|
|
|
</pre></body>
|
|
</html>
|