Letter case permutation by using backtracking
Here given code implementation process.
/*
Java Program for
Letter case permutation by using backtracking
*/
public class Permutation
{
// The letter case permutation is
// based on a lowercase and uppercase conversion.
public void letterPermutation(
String text,
String result,
int index, int n)
{
if (index < 0)
{
if (result.length() == n)
{
System.out.println(" " + result);
}
return;
}
letterPermutation(text, text.charAt(index) + result,
index - 1, n);
if ((text.charAt(index) >= '0' &&
text.charAt(index) <= '9') == false)
{
// When not digit
if (text.charAt(index) >= 'a' &&
text.charAt(index) <= 'z')
{
// Value of index is small character
// Change to capital letter.
letterPermutation(text,
(char)(text.charAt(index) - 32) + result,
index - 1, n);
}
else if (text.charAt(index) >= 'A' &&
text.charAt(index) <= 'Z')
{
// Value of index is capital character
// Change to small letter.
letterPermutation(text,
(char)(text.charAt(index) + 32) + result,
index - 1, n);
}
}
}
public static void main(String[] args)
{
Permutation task = new Permutation();
// Input text which includes character and number
String text = "a1b2c3d";
// Get the length of text
int n = text.length();
// Test
task.letterPermutation(text, "", n - 1, n);
}
}
Output
a1b2c3d
A1b2c3d
a1B2c3d
A1B2c3d
a1b2C3d
A1b2C3d
a1B2C3d
A1B2C3d
a1b2c3D
A1b2c3D
a1B2c3D
A1B2c3D
a1b2C3D
A1b2C3D
a1B2C3D
A1B2C3D
// Include header file
#include <iostream>
#include <string>
using namespace std;
/*
C++ Program for
Letter case permutation by using backtracking
*/
class Permutation
{
public:
// The letter case permutation is
// based on a lowercase and uppercase conversion.
void letterPermutation(
string text,
string result,
int index,
int n)
{
if (index < 0)
{
if (result.length() == n)
{
cout << " " << result << endl;
}
return;
}
this->letterPermutation(text, text[index] +
result, index - 1, n);
if ((text[index] >= '0' && text[index] <= '9') == false)
{
// When not digit
if (text[index] >= 'a' && text[index] <= 'z')
{
// Value of index is small character
// Change to capital letter.
this->letterPermutation(text,
((char)(text[index] - 32))
+ result, index - 1, n);
}
else if (text[index] >= 'A' && text[index] <= 'Z')
{
// Value of index is capital character
// Change to small letter.
this->letterPermutation(text,
((char)(text[index] + 32))
+ result, index - 1, n);
}
}
}
};
int main()
{
Permutation *task = new Permutation();
// Input text which includes character and number
string text = "a1b2c3d";
// Get the length of text
int n = text.length();
// Test
task->letterPermutation(text, "", n - 1, n);
return 0;
}
Output
a1b2c3d
A1b2c3d
a1B2c3d
A1B2c3d
a1b2C3d
A1b2C3d
a1B2C3d
A1B2C3d
a1b2c3D
A1b2c3D
a1B2c3D
A1B2c3D
a1b2C3D
A1b2C3D
a1B2C3D
A1B2C3D
// Include namespace system
using System;
/*
Csharp Program for
Letter case permutation by using backtracking
*/
public class Permutation
{
// The letter case permutation is
// based on a lowercase and uppercase conversion.
public void letterPermutation(
String text,
String result,
int index,
int n)
{
if (index < 0)
{
if (result.Length == n)
{
Console.WriteLine(" " + result);
}
return;
}
this.letterPermutation(text, text[index] + result, index - 1, n);
if ((text[index] >= '0' && text[index] <= '9') == false)
{
// When not digit
if (text[index] >= 'a' && text[index] <= 'z')
{
// Value of index is small character
// Change to capital letter.
this.letterPermutation(text,
(char)(text[index] - 32) + result,
index - 1, n);
}
else if (text[index] >= 'A' && text[index] <= 'Z')
{
// Value of index is capital character
// Change to small letter.
this.letterPermutation(text,
(char)(text[index] + 32) + result,
index - 1, n);
}
}
}
public static void Main(String[] args)
{
Permutation task = new Permutation();
// Input text which includes character and number
String text = "a1b2c3d";
// Get the length of text
int n = text.Length;
// Test
task.letterPermutation(text, "", n - 1, n);
}
}
Output
a1b2c3d
A1b2c3d
a1B2c3d
A1B2c3d
a1b2C3d
A1b2C3d
a1B2C3d
A1B2C3d
a1b2c3D
A1b2c3D
a1B2c3D
A1B2c3D
a1b2C3D
A1b2C3D
a1B2C3D
A1B2C3D
<?php
/*
Php Program for
Letter case permutation by using backtracking
*/
class Permutation
{
// The letter case permutation is
// based on a lowercase and uppercase conversion.
public function letterPermutation($text, $result, $index, $n)
{
if ($index < 0)
{
if (strlen($result) == $n)
{
echo(" ".$result."\n");
}
return;
}
$this->letterPermutation($text,
strval($text[$index]).$result,
$index - 1, $n);
if (($text[$index] >= '0' && $text[$index] <= '9') == false)
{
// When not digit
if ($text[$index] >= 'a' && $text[$index] <= 'z')
{
// Value of index is small character
// Change to capital letter.
$this->letterPermutation($text,
strval(chr((ord($text[$index]) - 32))).$result,
$index - 1, $n);
}
else if ($text[$index] >= 'A' && $text[$index] <= 'Z')
{
// Value of index is capital character
// Change to small letter.
$this->letterPermutation($text,
strval(chr((ord($text[$index]) + 32))).$result,
$index - 1, $n);
}
}
}
}
function main()
{
$task = new Permutation();
// Input text which includes character and number
$text = "a1b2c3d";
// Get the length of text
$n = strlen($text);
// Test
$task->letterPermutation($text, "", $n - 1, $n);
}
main();
Output
a1b2c3d
A1b2c3d
a1B2c3d
A1B2c3d
a1b2C3d
A1b2C3d
a1B2C3d
A1B2C3d
a1b2c3D
A1b2c3D
a1B2c3D
A1B2c3D
a1b2C3D
A1b2C3D
a1B2C3D
A1B2C3D
package main
import "fmt"
/*
Go Program for
Letter case permutation by using backtracking
*/
// The letter case permutation is
// based on a lowercase and uppercase conversion.
func letterPermutation(text string,
result string,
index int,
n int) {
if index < 0 {
if len(result) == n {
fmt.Println(" ", result)
}
return
}
letterPermutation(text, string(text[index]) + result,
index - 1, n)
if (text[index] >= '0' && text[index] <= '9') == false {
// When not digit
if text[index] >= 'a' && text[index] <= 'z' {
// Value of index is small character
// Change to capital letter.
letterPermutation(text,
string((byte)(text[index] - 32)) + result,
index - 1, n)
} else if text[index] >= 'A' && text[index] <= 'Z' {
// Value of index is capital character
// Change to small letter.
letterPermutation(text,
string((byte)(text[index] + 32)) + result,
index - 1, n)
}
}
}
func main() {
// Input text which includes character and number
var text string = "a1b2c3d"
// Get the length of text
var n int = len(text)
// Test
letterPermutation(text, "", n - 1, n)
}
Output
a1b2c3d
A1b2c3d
a1B2c3d
A1B2c3d
a1b2C3d
A1b2C3d
a1B2C3d
A1B2C3d
a1b2c3D
A1b2c3D
a1B2c3D
A1B2c3D
a1b2C3D
A1b2C3D
a1B2C3D
A1B2C3D
/*
Node JS Program for
Letter case permutation by using backtracking
*/
class Permutation
{
// The letter case permutation is
// based on a lowercase and uppercase conversion.
letterPermutation(text, result, index, n)
{
if (index < 0)
{
if (result.length == n)
{
console.log(" " + result);
}
return;
}
this.letterPermutation(text,
text.charAt(index) + result,
index - 1, n);
if ((text.charAt(index) >= '0' &&
text.charAt(index) <= '9') == false)
{
// When not digit
if (text.charAt(index) >= 'a' &&
text.charAt(index) <= 'z')
{
// Value of index is small character
// Change to capital letter.
this.letterPermutation(text,
String.fromCharCode((text.charCodeAt(index)- 32)) + result,
index - 1, n);
}
else if (text.charAt(index) >= 'A' && text.charAt(index) <= 'Z')
{
// Value of index is capital character
// Change to small letter.
this.letterPermutation(text,
String.fromCharCode((text.charCodeAt(index) + 32)) + result,
index - 1, n);
}
}
}
}
function main()
{
var task = new Permutation();
// Input text which includes character and number
var text = "a1b2c3d";
// Get the length of text
var n = text.length;
// Test
task.letterPermutation(text, "", n - 1, n);
}
main();
Output
a1b2c3d
A1b2c3d
a1B2c3d
A1B2c3d
a1b2C3d
A1b2C3d
a1B2C3d
A1B2C3d
a1b2c3D
A1b2c3D
a1B2c3D
A1B2c3D
a1b2C3D
A1b2C3D
a1B2C3D
A1B2C3D
# Python 3 Program for
# Letter case permutation by using backtracking
class Permutation :
# The letter case permutation is
# based on a lowercase and uppercase conversion.
def letterPermutation(self, text, result, index, n) :
if (index < 0) :
if (len(result) == n) :
print(" ", result)
return
self.letterPermutation(text, str(text[index]) +
result, index - 1, n)
if ((text[index] >= '0'
and text[index] <= '9') == False) :
# When not digit
if (text[index] >= 'a'
and text[index] <= 'z') :
# Value of index is small character
# Change to capital letter.
self.letterPermutation(text,
str(chr((ord(text[index]) - 32))) +
result, index - 1, n)
elif (text[index] >= 'A'
and text[index] <= 'Z') :
# Value of index is capital character
# Change to small letter.
self.letterPermutation(text,
str(chr((ord(text[index]) + 32))) +
result, index - 1, n)
def main() :
task = Permutation()
# Input text which includes character and number
text = "a1b2c3d"
# Get the length of text
n = len(text)
# Test
task.letterPermutation(text, "", n - 1, n)
if __name__ == "__main__": main()
Output
a1b2c3d
A1b2c3d
a1B2c3d
A1B2c3d
a1b2C3d
A1b2C3d
a1B2C3d
A1B2C3d
a1b2c3D
A1b2c3D
a1B2c3D
A1B2c3D
a1b2C3D
A1b2C3D
a1B2C3D
A1B2C3D
# Ruby Program for
# Letter case permutation by using backtracking
class Permutation
# The letter case permutation is
# based on a lowercase and uppercase conversion.
def letterPermutation(text, result, index, n)
if (index < 0)
if (result.length == n)
print(" ", result, "\n")
end
return
end
self.letterPermutation(text,
text[index].to_s + result,
index - 1, n)
if ((text[index] >= '0' && text[index] <= '9') == false)
# When not digit
if (text[index] >= 'a' && text[index] <= 'z')
# Value of index is small character
# Change to capital letter.
self.letterPermutation(text,
((text[index].ord - 32)).chr.to_s + result,
index - 1, n)
elsif (text[index] >= 'A' && text[index] <= 'Z')
# Value of index is capital character
# Change to small letter.
self.letterPermutation(text,
((text[index].ord + 32)).chr.to_s + result,
index - 1, n)
end
end
end
end
def main()
task = Permutation.new()
# Input text which includes character and number
text = "a1b2c3d"
# Get the length of text
n = text.length
# Test
task.letterPermutation(text, "", n - 1, n)
end
main()
Output
a1b2c3d
A1b2c3d
a1B2c3d
A1B2c3d
a1b2C3d
A1b2C3d
a1B2C3d
A1B2C3d
a1b2c3D
A1b2c3D
a1B2c3D
A1B2c3D
a1b2C3D
A1b2C3D
a1B2C3D
A1B2C3D
import scala.collection.mutable._;
/*
Scala Program for
Letter case permutation by using backtracking
*/
class Permutation()
{
// The letter case permutation is
// based on a lowercase and uppercase conversion.
def letterPermutation(
text: String,
result: String,
index: Int,
n: Int): Unit = {
if (index < 0)
{
if (result.length() == n)
{
println(" " + result);
}
return;
}
letterPermutation(text,
text.charAt(index).toString() + result, index - 1, n);
if ((text.charAt(index) >= '0' &&
text.charAt(index) <= '9') == false)
{
// When not digit
if (text.charAt(index) >= 'a' &&
text.charAt(index) <= 'z')
{
// Value of index is small character
// Change to capital letter.
letterPermutation(text,
(text.charAt(index).toInt -
32).toChar.toString() + result,
index - 1, n);
}
else if (text.charAt(index) >= 'A' &&
text.charAt(index) <= 'Z')
{
// Value of index is capital character
// Change to small letter.
letterPermutation(text,
(text.charAt(index).toInt +
32).toChar.toString() + result, index - 1, n);
}
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Permutation = new Permutation();
// Input text which includes character and number
var text: String = "a1b2c3d";
// Get the length of text
var n: Int = text.length();
// Test
task.letterPermutation(text, "", n - 1, n);
}
}
Output
a1b2c3d
A1b2c3d
a1B2c3d
A1B2c3d
a1b2C3d
A1b2C3d
a1B2C3d
A1B2C3d
a1b2c3D
A1b2c3D
a1B2c3D
A1B2c3D
a1b2C3D
A1b2C3D
a1B2C3D
A1B2C3D
import Foundation;
/*
Swift 4 Program for
Letter case permutation by using backtracking
*/
class Permutation
{
// The letter case permutation is
// based on a lowercase and uppercase conversion.
func letterPermutation(
_ text: [Character],
_ result: String,
_ index: Int,
_ n: Int)
{
if (index < 0)
{
if (result.count == n)
{
print(" ", result);
}
return;
}
self.letterPermutation(text,
String(text[index]) + result,
index - 1, n);
if ((text[index] >= "0" && text[index] <= "9") == false)
{
// When not digit
if (text[index] >= "a" && text[index] <= "z")
{
let v = String(format: "%c",
(Int(UnicodeScalar(String(text[index]))!.value) -
32)) as String;
// Value of index is small character
// Change to capital letter.
self.letterPermutation(text, v + result, index - 1, n);
}
else if (text[index] >= "A" && text[index] <= "Z")
{
let v = String(format: "%c",
(Int(UnicodeScalar(String(text[index]))!.value) +
32)) as String;
// Value of index is capital character
// Change to small letter.
self.letterPermutation(text,v + result, index - 1, n);
}
}
}
}
func main()
{
let task: Permutation = Permutation();
// Input text which includes character and number
let text: String = "a1b2c3d";
// Get the length of text
let n: Int = text.count;
// Test
task.letterPermutation(Array(text), "", n - 1, n);
}
main();
Output
a1b2c3d
A1b2c3d
a1B2c3d
A1B2c3d
a1b2C3d
A1b2C3d
a1B2C3d
A1B2C3d
a1b2c3D
A1b2c3D
a1B2c3D
A1B2c3D
a1b2C3D
A1b2C3D
a1B2C3D
A1B2C3D
/*
Kotlin Program for
Letter case permutation by using backtracking
*/
class Permutation
{
// The letter case permutation is
// based on a lowercase and uppercase conversion.
fun letterPermutation(
text: String, result: String,
index: Int, n: Int): Unit
{
if (index < 0)
{
if (result.length == n)
{
println(" " + result);
}
return;
}
this.letterPermutation(text,
text.get(index).toString() + result,
index - 1, n);
if ((text.get(index) >= '0' && text.get(index) <= '9') == false)
{
// When not digit
if (text.get(index) >= 'a' && text.get(index) <= 'z')
{
// Value of index is small character
// Change to capital letter.
this.letterPermutation(text,
(text.get(index).toInt() -
32).toChar().toString() + result,
index - 1, n);
}
else if (text.get(index) >= 'A' && text.get(index) <= 'Z')
{
// Value of index is capital character
// Change to small letter.
this.letterPermutation(text,
(text.get(index).toInt() +
32).toChar().toString() + result,
index - 1, n);
}
}
}
}
fun main(args: Array < String > ): Unit
{
val task: Permutation = Permutation();
// Input text which includes character and number
val text: String = "a1b2c3d";
// Get the length of text
val n: Int = text.length;
// Test
task.letterPermutation(text, "", n - 1, n);
}
Output
a1b2c3d
A1b2c3d
a1B2c3d
A1B2c3d
a1b2C3d
A1b2C3d
a1B2C3d
A1B2C3d
a1b2c3D
A1b2c3D
a1B2c3D
A1B2c3D
a1b2C3D
A1b2C3D
a1B2C3D
A1B2C3D
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