Given a string S print the number of unique string values that can be formed by rearranging the letters in the string S.
Boundary Condition:
2 <= Length of S <= 22
Input/Output Format:
Input:
First line contains the string S.
Output:
The value representing the number of unique string values that can be formed by rearranging the letters in the string S.
Example Input/Output 1:
Input:
abc
Output:
6
Example Input/Output 2:
Input:
apple
Output:
60
# The program calculates the Unique String Representations.
def main():
s = input() # Read the input string
l = len(s)
x, t, flag = 1, 1, 0
# Iterate through the string and count the occurrences of each character
for i in range(l):
c = 1
for j in range(i + 1, l):
if s[i] == s[j] and s[j] != '*':
c += 1
s[j] = '*' # Mark duplicate characters with '*'
s[i] = '*' # Mark the current character with '*'
# Calculate the factorial of the count of each character
if c != 1:
m = 1
for k in range(1, c + 1):
m *= k
t *= m
# Calculate the factorial of the length of the string
for i in range(1, l + 1):
if i % 10 == 0:
flag += 1
else:
x *= i
if i == 20:
x *= 2
# Calculate the result and print it
result = x // t
print(result, end='')
# Print trailing zeros based on the count of zeros in 'flag'
if flag == 1:
print("0", end='')
elif flag == 2:
print("00", end='')
if __name__ == "__main__":
main()
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main() {
char s[22];
cin >> s; // Read the input string
unsigned long long l = strlen(s);
unsigned long long x = 1, t = 1, flag = 0;
// Iterate through the string and count the occurrences of each character
for (unsigned long long i = 0; i < l; i++) {
unsigned long long c = 1;
for (unsigned long long j = i + 1; j < l; j++) {
if (s[i] == s[j] && s[j] != '*') {
c++;
s[j] = '*'; // Mark duplicate characters with '*'
}
}
s[i] = '*'; // Mark the current character with '*'
// Calculate the factorial of the count of each character
if (c != 1) {
unsigned long long m = 1;
for (int k = 1; k <= c; k++) {
m *= k;
}
t *= m;
}
}
// Calculate the factorial of the length of the string
for (unsigned long long i = 1; i <= l; i++) {
if (i % 10 == 0) {
flag++;
} else {
x *= i;
}
if (i == 20) {
x *= 2;
}
}
// Calculate the result and print it
unsigned long long result = x / t;
cout << result;
// Print trailing zeros based on the count of zeros in 'flag'
if (flag == 1) cout << "0";
if (flag == 2) cout << "00";
return 0;
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String s = scanner.next(); // Read the input string
int l = s.length();
long x = 1, t = 1, flag = 0;
char[] arr = s.toCharArray();
// Iterate through the string and count the occurrences of each character
for (int i = 0; i < l; i++) {
int c = 1;
for (int j = i + 1; j < l; j++) {
if (arr[i] == arr[j] && arr[j] != '*') {
c++;
arr[j] = '*'; // Mark duplicate characters with '*'
}
}
arr[i] = '*'; // Mark the current character with '*'
// Calculate the factorial of the count of each character
if (c != 1) {
long m = 1;
for (int k = 1; k <= c; k++) {
m *= k;
}
t *= m;
}
}
// Calculate the factorial of the length of the string
for (int i = 1; i <= l; i++) {
if (i % 10 == 0) {
flag++;
} else {
x *= i;
}
if (i == 20) {
x *= 2;
}
}
// Calculate the result and print it
long result = x / t;
System.out.print(result);
// Print trailing zeros based on the count of zeros in 'flag'
if (flag == 1) System.out.print("0");
if (flag == 2) System.out.print("00");
}
}
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('Enter the string: ', s => {
const l = s.length;
let x = 1, t = 1, flag = 0;
const arr = s.split('');
// Iterate through the string and count the occurrences of each character
for (let i = 0; i < l; i++) {
let c = 1;
for (let j = i + 1; j < l; j++) {
if (arr[i] === arr[j] && arr[j] !== '*') {
c++;
arr[j] = '*'; // Mark duplicate characters with '*'
}
}
arr[i] = '*'; // Mark the current character with '*'
// Calculate the factorial of the count of each character
if (c !== 1) {
let m = 1;
for (let k = 1; k <= c; k++) {
m *= k;
}
t *= m;
}
}
// Calculate the factorial of the length of the string
for (let i = 1; i <= l; i++) {
if (i % 10 === 0) {
flag++;
} else {
x *= i;
}
if (i === 20) {
x *= 2;
}
}
// Calculate the result and print it
const result = x / t;
process.stdout.write(result.toString());
// Print trailing zeros based on the count of zeros in 'flag'
if (flag === 1) process.stdout.write("0");
if (flag === 2) process.stdout.write("00");
rl.close();
});
using System;
public class Program {
public static void Main() {
string s = Console.ReadLine(); // Read the input string
int l = s.Length;
long x = 1, t = 1, flag = 0;
char[] arr = s.ToCharArray();
// Iterate through the string and count the occurrences of each character
for (int i = 0; i < l; i++) {
int c = 1;
for (int j = i + 1; j < l; j++) {
if (arr[i] == arr[j] && arr[j] != '*') {
c++;
arr[j] = '*'; // Mark duplicate characters with '*'
}
}
arr[i] = '*'; // Mark the current character with '*'
// Calculate the factorial of the count of each character
if (c != 1) {
long m = 1;
for (int k = 1; k <= c; k++) {
m *= k;
}
t *= m;
}
}
// Calculate the factorial of the length of the string
for (int i = 1; i <= l; i++) {
if (i % 10 == 0) {
flag++;
} else {
x *= i;
}
if (i == 20) {
x *= 2;
}
}
// Calculate the result and print it
long result = x / t;
Console.Write(result);
// Print trailing zeros based on the count of zeros in 'flag'
if (flag == 1) Console.Write("0");
if (flag == 2) Console.Write("00");
}
}
#include <stdio.h>
#include <string.h>
int main() {
char s[22];
scanf("%s", s); // Read the input string
unsigned long long l = strlen(s);
unsigned long long x = 1, t = 1, flag = 0;
// Iterate through the string and count the occurrences of each character
for (unsigned long long i = 0; i < l; i++) {
unsigned long long c = 1;
for (unsigned long long j = i + 1; j < l; j++) {
if (s[i] == s[j] && s[j] != '*') {
c++;
s[j] = '*'; // Mark duplicate characters with '*'
}
}
s[i] = '*'; // Mark the current character with '*'
// Calculate the factorial of the count of each character
if (c != 1) {
unsigned long long m = 1;
for (int k = 1; k <= c; k++) {
m *= k;
}
t *= m;
}
}
// Calculate the factorial of the length of the string
for (unsigned long long i = 1; i <= l; i++) {
if (i % 10 == 0) {
flag++;
} else {
x *= i;
}
if (i == 20) {
x *= 2;
}
}
// Calculate the result and print it
unsigned long long result = x / t;
printf("%llu", result);
// Print trailing zeros based on the count of zeros in 'flag'
if (flag == 1) printf("0");
if (flag == 2) printf("00");
return 0;
}