Print all possible strings of length N in a set
Here given code implementation process.
/*
Java program
Print all possible strings of length N in a set
*/
public class Combinations
{
public void result(char[] record, String output, int n, int size)
{
if (n == 0)
{
// Display output
System.out.println(output);
return;
}
for (int i = 0; i < size; ++i)
{
// Find result using recursion
result(record, output + record[i], n - 1, size);
}
}
public static void main(String[] args)
{
Combinations task = new Combinations();
// record element
char[] record = {
'A' , 'B'
};
// Get number of element in record set
int size = record.length;
// Length of generated string
int n = 4;
task.result(record, "", n, size);
}
}
input
AAAA
AAAB
AABA
AABB
ABAA
ABAB
ABBA
ABBB
BAAA
BAAB
BABA
BABB
BBAA
BBAB
BBBA
BBBB
// Include header file
#include <iostream>
using namespace std;
/*
C++ program
Print all possible strings of length N in a set
*/
class Combinations
{
public: void result(char record[], string output, int n, int size)
{
if (n == 0)
{
// Display output
cout << output << endl;
return;
}
for (int i = 0; i < size; ++i)
{
// Find result using recursion
this->result(record, output + record[i], n - 1, size);
}
}
};
int main()
{
Combinations *task = new Combinations();
// record element
char record[] = {
'A' , 'B'
};
// Get number of element in record set
int size = sizeof(record) / sizeof(record[0]);
// Length of generated string
int n = 4;
task->result(record, "", n, size);
return 0;
}
input
AAAA
AAAB
AABA
AABB
ABAA
ABAB
ABBA
ABBB
BAAA
BAAB
BABA
BABB
BBAA
BBAB
BBBA
BBBB
// Include namespace system
using System;
/*
Csharp program
Print all possible strings of length N in a set
*/
public class Combinations
{
public void result(char[] record, String output, int n, int size)
{
if (n == 0)
{
// Display output
Console.WriteLine(output);
return;
}
for (int i = 0; i < size; ++i)
{
// Find result using recursion
this.result(record, output + record[i], n - 1, size);
}
}
public static void Main(String[] args)
{
Combinations task = new Combinations();
// record element
char[] record = {
'A' , 'B'
};
// Get number of element in record set
int size = record.Length;
// Length of generated string
int n = 4;
task.result(record, "", n, size);
}
}
input
AAAA
AAAB
AABA
AABB
ABAA
ABAB
ABBA
ABBB
BAAA
BAAB
BABA
BABB
BBAA
BBAB
BBBA
BBBB
package main
import "fmt"
/*
Go program
Print all possible strings of length N in a set
*/
func result(record[] byte, output string, n int, size int) {
if n == 0 {
// Display output
fmt.Println(output)
return
}
for i := 0 ; i < size ; i++ {
// Find result using recursion
result(record, output + string(record[i]), n - 1, size)
}
}
func main() {
// record element
var record = [] byte {
'A',
'B',
}
// Get number of element in record set
var size int = len(record)
// Length of generated string
var n int = 4
result(record, "", n, size)
}
input
AAAA
AAAB
AABA
AABB
ABAA
ABAB
ABBA
ABBB
BAAA
BAAB
BABA
BABB
BBAA
BBAB
BBBA
BBBB
<?php
/*
Php program
Print all possible strings of length N in a set
*/
class Combinations
{
public function result($record, $output, $n, $size)
{
if ($n == 0)
{
// Display output
echo($output.
"\n");
return;
}
for ($i = 0; $i < $size; ++$i)
{
// Find result using recursion
$this->result($record, $output.strval($record[$i]), $n - 1, $size);
}
}
}
function main()
{
$task = new Combinations();
// record element
$record = array('A', 'B');
// Get number of element in record set
$size = count($record);
// Length of generated string
$n = 4;
$task->result($record, "", $n, $size);
}
main();
input
AAAA
AAAB
AABA
AABB
ABAA
ABAB
ABBA
ABBB
BAAA
BAAB
BABA
BABB
BBAA
BBAB
BBBA
BBBB
/*
Node JS program
Print all possible strings of length N in a set
*/
class Combinations
{
result(record, output, n, size)
{
if (n == 0)
{
// Display output
console.log(output);
return;
}
for (var i = 0; i < size; ++i)
{
// Find result using recursion
this.result(record, output + record[i], n - 1, size);
}
}
}
function main()
{
var task = new Combinations();
// record element
var record = ['A', 'B'];
// Get number of element in record set
var size = record.length;
// Length of generated string
var n = 4;
task.result(record, "", n, size);
}
main();
input
AAAA
AAAB
AABA
AABB
ABAA
ABAB
ABBA
ABBB
BAAA
BAAB
BABA
BABB
BBAA
BBAB
BBBA
BBBB
# Python 3 program
# Print all possible strings of length N in a set
class Combinations :
def result(self, record, output, n, size) :
if (n == 0) :
# Display output
print(output)
return
i = 0
while (i < size) :
# Find result using recursion
self.result(record, output + str(record[i]), n - 1, size)
i += 1
def main() :
task = Combinations()
# record element
record = ['A', 'B']
# Get number of element in record set
size = len(record)
# Length of generated string
n = 4
task.result(record, "", n, size)
if __name__ == "__main__": main()
input
AAAA
AAAB
AABA
AABB
ABAA
ABAB
ABBA
ABBB
BAAA
BAAB
BABA
BABB
BBAA
BBAB
BBBA
BBBB
# Ruby program
# Print all possible strings of length N in a set
class Combinations
def result(record, output, n, size)
if (n == 0)
# Display output
print(output, "\n")
return
end
i = 0
while (i < size)
# Find result using recursion
self.result(record, output + record[i].to_s, n - 1, size)
i += 1
end
end
end
def main()
task = Combinations.new()
# record element
record = ['A', 'B']
# Get number of element in record set
size = record.length
# Length of generated string
n = 4
task.result(record, "", n, size)
end
main()
input
AAAA
AAAB
AABA
AABB
ABAA
ABAB
ABBA
ABBB
BAAA
BAAB
BABA
BABB
BBAA
BBAB
BBBA
BBBB
/*
Scala program
Print all possible strings of length N in a set
*/
class Combinations()
{
def result(record: Array[Char], output: String, n: Int, size: Int): Unit = {
if (n == 0)
{
// Display output
println(output);
return;
}
var i: Int = 0;
while (i < size)
{
// Find result using recursion
result(record, output + record(i).toString(), n - 1, size);
i += 1;
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Combinations = new Combinations();
// record element
var record: Array[Char] = Array('A', 'B');
// Get number of element in record set
var size: Int = record.length;
// Length of generated string
var n: Int = 4;
task.result(record, "", n, size);
}
}
input
AAAA
AAAB
AABA
AABB
ABAA
ABAB
ABBA
ABBB
BAAA
BAAB
BABA
BABB
BBAA
BBAB
BBBA
BBBB
import Foundation;
/*
Swift 4 program
Print all possible strings of length N in a set
*/
class Combinations
{
func result(_ record: [Character],
_ output: String, _ n: Int, _ size: Int)
{
if (n == 0)
{
// Display output
print(output);
return;
}
var i: Int = 0;
while (i < size)
{
// Find result using recursion
self.result(record, output + String(record[i]), n - 1, size);
i += 1;
}
}
}
func main()
{
let task: Combinations = Combinations();
// record element
let record: [Character] = ["A", "B"];
// Get number of element in record set
let size: Int = record.count;
// Length of generated string
let n: Int = 4;
task.result(record, "", n, size);
}
main();
input
AAAA
AAAB
AABA
AABB
ABAA
ABAB
ABBA
ABBB
BAAA
BAAB
BABA
BABB
BBAA
BBAB
BBBA
BBBB
/*
Kotlin program
Print all possible strings of length N in a set
*/
class Combinations
{
fun result(record: Array < Char > ,
output: String, n: Int, size: Int): Unit
{
if (n == 0)
{
// Display output
println(output);
return;
}
var i: Int = 0;
while (i < size)
{
// Find result using recursion
this.result(record, output + record[i].toString(), n - 1, size);
i += 1;
}
}
}
fun main(args: Array < String > ): Unit
{
val task: Combinations = Combinations();
// record element
val record: Array < Char > = arrayOf('A', 'B');
// Get number of element in record set
val size: Int = record.count();
// Length of generated string
val n: Int = 4;
task.result(record, "", n, size);
}
input
AAAA
AAAB
AABA
AABB
ABAA
ABAB
ABBA
ABBB
BAAA
BAAB
BABA
BABB
BBAA
BBAB
BBBA
BBBB
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment