Count of distinct permutation of a String obtained by swapping only unequal characters
Last Updated :
12 Nov, 2023
Given a string find the number of unique permutations that can be obtained by swapping two indices such that the elements at these indices are distinct.
NOTE: Swapping is always performed in the original string.
Examples:
Input: str = "sstt"
Output: 5
Explanation:
Swap str[0] with str[2], string obtained "tsst" which is valid (str[0]!=str[2])
Swap str[0] with str[3]. string obtained "tsts"
Swap str[1] with str[2], string obtained "stst"
Swap str[1] with str[3], string obtained "stts"
Hence total 5 strings can be obtained including the given string ("sstt")
Input: str = "abcd"
Output: 7
Naive Approach:
- Create a set to store unique strings
- Loop through each pair of indices in the given string
- Check if the elements at the pair of indices are distinct
- If the elements are distinct, swap the elements at those indices and add the resulting string to the set
- Swap the elements back to their original positions
- Add 1 to count the original string
- Return the number of unique permutations, including the original string.
Below is the implementation of the above approach:
C++
#include <iostream>
#include <set>
#include <string>
using namespace std;
int countUniquePermutations(string str)
{
// create a set to store unique strings
set<string> uniqueStrings;
// loop through each pair of indices
// in the given string
for (int i = 0; i < str.length(); i++) {
for (int j = i + 1; j < str.length(); j++) {
// check if the elements at the
// pair of indices are distinct
if (str[i] != str[j]) {
swap(str[i], str[j]);
uniqueStrings.insert(str);
swap(str[i], str[j]);
}
}
}
// add 1 to count the original string
// and return number of unique permutations
return uniqueStrings.size() + 1;
}
int main()
{
string str = "sstt";
cout << countUniquePermutations(str);
return 0;
}
Java
import java.util.HashSet;
import java.util.Set;
public class UniquePermutations {
public static int countUniquePermutations(String str)
{
// Create a set to store unique strings
Set<String> uniqueStrings = new HashSet<>();
// Loop through each pair of indices in the given
// string
for (int i = 0; i < str.length(); i++) {
for (int j = i + 1; j < str.length(); j++) {
// Check if the elements at the pair of
// indices are distinct
if (str.charAt(i) != str.charAt(j)) {
// Swap the characters at the indices
char[] strArray = str.toCharArray();
char temp = strArray[i];
strArray[i] = strArray[j];
strArray[j] = temp;
String swappedStr
= new String(strArray);
// Add the swapped string to the set
uniqueStrings.add(swappedStr);
// Swap back to the original string
temp = strArray[i];
strArray[i] = strArray[j];
strArray[j] = temp;
}
}
}
// Add 1 to count the original string and return the
// number of unique permutations
return uniqueStrings.size() + 1;
}
public static void main(String[] args)
{
String str = "sstt";
System.out.println(countUniquePermutations(str));
}
}
Python3
def count_unique_permutations(s):
# Create a set to store unique strings
unique_strings = set()
# Loop through each pair of indices in the given string
for i in range(len(s)):
for j in range(i + 1, len(s)):
# Check if the elements at the pair of indices are distinct
if s[i] != s[j]:
# Swap the elements at the indices to generate a new string
# and add it to the set
s_list = list(s)
s_list[i], s_list[j] = s_list[j], s_list[i]
unique_strings.add("".join(s_list))
# Add 1 to count the original string and return the number of unique permutations
return len(unique_strings) + 1
# Driver Code
if __name__ == "__main__":
input_str = "sstt"
print(count_unique_permutations(input_str))
C#
using System;
using System.Collections.Generic;
class Program
{
static int CountUniquePermutations(string str)
{
// Create a HashSet to store unique strings
HashSet<string> uniqueStrings = new HashSet<string>();
// Loop through each pair of indices in the given string
for (int i = 0; i < str.Length; i++)
{
for (int j = i + 1; j < str.Length; j++)
{
// Check if the elements at the pair of indices are distinct
if (str[i] != str[j])
{
char[] chars = str.ToCharArray();
// Swap the elements at indices i and j
char temp = chars[i];
chars[i] = chars[j];
chars[j] = temp;
string newString = new string(chars);
// Insert the unique string into the HashSet
uniqueStrings.Add(newString);
// Swap back to restore the original string
temp = chars[i];
chars[i] = chars[j];
chars[j] = temp;
}
}
}
// Add 1 to count the original string and return the number of unique permutations
return uniqueStrings.Count + 1;
}
static void Main()
{
string str = "sstt";
Console.WriteLine(CountUniquePermutations(str));
}
}
JavaScript
function countUniquePermutations(str) {
// Create a Set to store unique strings
let uniqueStrings = new Set();
// Loop through each pair of indices in the given string
for (let i = 0; i < str.length; i++) {
for (let j = i + 1; j < str.length; j++) {
// Check if the elements at the pair of indices are distinct
if (str[i] !== str[j]) {
// Swap the characters at indices i and j
let strArray = str.split('');
[strArray[i], strArray[j]] = [strArray[j], strArray[i]];
let modifiedStr = strArray.join('');
uniqueStrings.add(modifiedStr);
// Swap the characters back to their original positions
[strArray[i], strArray[j]] = [strArray[j], strArray[i]];
}
}
}
// Add 1 to count the original string and return the number of unique permutations
return uniqueStrings.size + 1;
}
let str = "sstt";
console.log(countUniquePermutations(str));
Time Complexity: The time complexity of this algorithm is O(n2 * log n), where n is the length of the input string. This is because we have two nested loops, and within each loop, we perform a constant-time set insertion operation, which has a time complexity of O(log n) on average. Therefore, the overall time complexity is O(n^2 * log n).
Auxiliary Space: The space complexity of this algorithm is also O(n2). This is because we create a set to store the unique strings, and the size of the set can be as large as n^2 in the worst case, if all possible string permutations are unique. Additionally, we store the input string itself, which has a size of n. Therefore, the overall space complexity is O(n^2 + n), which simplifies to O(n^2).
Efficient Approach: The problem can be solved using HashMap by the following steps:
- Create a hashmap and store the frequency of every character of the given string.
- Create a variable count, to store the total number of characters in the given string, i.e. count=str.length() and a variable ans to store the number of unique permutations possible and initialize ans=0.
- Traverse the string and for each character:
- Find the number of different characters present in the right of the current index. This can be done by subtracting the frequency of that character by the total count.
- Now add this calculated value to ans, as this is the number of characters with which the current character can be swapped to create a unique permutation.
- Now, Reduce the frequency of the current character and count by 1, so that it cannot interfere with the calculations of the same elements present to the right of it.
- Return ans+1, because the given string is also a unique permutation.
C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to calculate total
// number of valid permutations
int validPermutations(string str)
{
unordered_map<char, int> m;
// Creating count which is equal to the
// Total number of characters present and
// ans that will store the number of unique
// permutations
int count = str.length(), ans = 0;
// Storing frequency of each character
// present in the string
for (int i = 0; i < str.length(); i++) {
m[str[i]]++;
}
for (int i = 0; i < str.length(); i++) {
// Adding count of characters by excluding
// characters equal to current char
ans += count - m[str[i]];
// Reduce the frequency of the current character
// and count by 1, so that it cannot interfere
// with the calculations of the same elements
// present to the right of it.
m[str[i]]--;
count--;
}
// Return ans+1 (Because the given string
// is also a unique permutation)
return ans + 1;
}
// Driver Code
int main()
{
string str = "sstt";
cout << validPermutations(str);
return 0;
}
Java
// Java program for the above approach
// Importing HashMap class
import java.util.HashMap;
class GFG {
// Function to calculate total
// number of valid permutations
static int validPermutations(String str)
{
HashMap<Character, Integer> m
= new HashMap<Character, Integer>();
// Creating count which is equal to the
// Total number of characters present and
// ans that will store the number of unique
// permutations
int count = str.length(), ans = 0;
// Storing frequency of each character
// present in the string
for (int i = 0; i < str.length(); i++) {
m.put(str.charAt(i),
m.getOrDefault(str.charAt(i), 0) + 1);
}
for (int i = 0; i < str.length(); i++) {
// Adding count of characters by excluding
// characters equal to current char
ans += count - m.get(str.charAt(i));
// Reduce the frequency of the current character
// and count by 1, so that it cannot interfere
// with the calculations of the same elements
// present to the right of it.
m.put(str.charAt(i), m.get(str.charAt(i)) - 1);
count--;
}
// Return ans+1 (Because the given string
// is also a unique permutation)
return ans + 1;
}
public static void main(String[] args)
{
String str = "sstt";
System.out.println(validPermutations(str));
}
}
// This code is contributed by rajsanghavi9.
Python3
# Python 3 program for the above approach
# Function to calculate total
# number of valid permutations
def validPermutations(str):
m = {}
# Creating count which is equal to the
# Total number of characters present and
# ans that will store the number of unique
# permutations
count = len(str)
ans = 0
# Storing frequency of each character
# present in the string
for i in range(len(str)):
if(str[i] in m):
m[str[i]] += 1
else:
m[str[i]] = 1
for i in range(len(str)):
# Adding count of characters by excluding
# characters equal to current char
ans += count - m[str[i]]
# Reduce the frequency of the current character
# and count by 1, so that it cannot interfere
# with the calculations of the same elements
# present to the right of it.
m[str[i]] -= 1
count -= 1
# Return ans+1 (Because the given string
# is also a unique permutation)
return ans + 1
# Driver Code
if __name__ == '__main__':
str = "sstt"
print(validPermutations(str))
# This code is contributed by SURENDRA_GANGWAR.
C#
// C# program for the above approach
// Importing Dictionary class
using System;
using System.Collections.Generic;
public class GFG {
// Function to calculate total
// number of valid permutations
static int validPermutations(String str)
{
Dictionary<char, int> m
= new Dictionary<char, int>();
// Creating count which is equal to the
// Total number of characters present and
// ans that will store the number of unique
// permutations
int count = str.Length, ans = 0;
// Storing frequency of each character
// present in the string
for (int i = 0; i < str.Length; i++) {
if(m.ContainsKey(str[i]))
m[str[i]]=m[str[i]]+1;
else
m.Add(str[i], 1);
}
for (int i = 0; i < str.Length; i++) {
// Adding count of characters by excluding
// characters equal to current char
ans += count - m[str[i]];
// Reduce the frequency of the current character
// and count by 1, so that it cannot interfere
// with the calculations of the same elements
// present to the right of it.
if(m.ContainsKey(str[i]))
m[str[i]]=m[str[i]]-1;
count--;
}
// Return ans+1 (Because the given string
// is also a unique permutation)
return ans + 1;
}
public static void Main(String[] args)
{
String str = "sstt";
Console.WriteLine(validPermutations(str));
}
}
// This code is contributed by shikhasingrajput
JavaScript
<script>
// JavaScript program for the above approach
// Function to calculate total
// number of valid permutations
function validPermutations(str) {
let m = new Map();
// Creating count which is equal to the
// Total number of characters present and
// ans that will store the number of unique
// permutations
let count = str.length,
ans = 0;
// Storing frequency of each character
// present in the string
for (let i = 0; i < str.length; i++) {
if (m.has(str[i])) {
m.set(str[i], m.get(str[i]) + 1);
} else {
m.set(str[i], 1);
}
}
for (let i = 0; i < str.length; i++)
{
// Adding count of characters by excluding
// characters equal to current char
ans += count - m.get(str[i]);
// Reduce the frequency of the current character
// and count by 1, so that it cannot interfere
// with the calculations of the same elements
// present to the right of it.
m.set(str[i], m.get(str[i]) - 1);
count--;
}
// Return ans+1 (Because the given string
// is also a unique permutation)
return ans + 1;
}
// Driver Code
let str = "sstt";
document.write(validPermutations(str));
</script>
Time Complexity: O(n)
Auxiliary Space: O(n)
Similar Reads
Count of distinct strings that can be obtained after performing exactly one swap
Given a string s containing lowercase English alphabet characters. The task is to calculate the number of distinct strings that can be obtained after performing exactly one swap. Input: s = "geek"Output: 6Explanation: Following are the strings formed by doing exactly one swapstrings = ["egek","eegk"
5 min read
Count new pairs of strings that can be obtained by swapping first characters of pairs of strings from given array
Given an array arr[] consisting of N strings, the task is to find the pair of strings that is not present in the array formed by any pairs (arr[i], arr[j]) by swapping the first characters of the strings arr[i] and arr[j]. Examples: Input: arr[] = {"good", "bad", "food"}Output: 2Explanation:The poss
13 min read
Count of distinct permutations of length N having no similar adjacent characters
Given an integer N, the task is to calculate the total number of distinct permutations of length N, consisting only of letters 'a', 'b', and 'c', with repetitions allowed, such that no two adjacent characters are the same. Input: N = 3 Output: 12 Explanation: Possible permutations satisfying the req
3 min read
Count of Distinct strings possible by inserting K characters in the original string
Given a string S and an integer K, the task is to find the total number of strings that can be formed by inserting exactly K characters at any position of the string S. Since the answer can be large, print it modulo 109+7.Examples: Input: S = "a" K = 1 Output: 51 Explanation: Since any of the 26 cha
12 min read
Sort an array of strings based on count of distinct characters
Given a string array arr[] as input, the task is to print the words sorted by number of distinct characters that occur in the word, followed by length of word. Note: If two words have same number of distinct characters, the word with more total characters comes first. If two words have same number o
6 min read
Count of Palindromic Strings possible by swapping of a pair of Characters
Given a palindromic string S, the task is to find the count of palindromic strings possible by swapping a pair of character at a time.Examples: Input: s = "abba" Output: 2 Explanation: 1st Swap: abba -> abba 2nd Swap: abba -> abba All other swaps will lead to a non-palindromic string. Therefor
4 min read
Count substrings made up of a single distinct character
Given a string S of length N, the task is to count the number of substrings made up of a single distinct character.Note: For the repetitive occurrences of the same substring, count all repetitions. Examples: Input: str = "geeksforgeeks"Output: 15Explanation: All substrings made up of a single distin
5 min read
Count the sum of count of distinct characters present in all Substrings
Given a string S consisting of lowercase English letters of size N where (1 <= N <= 105), the task is to print the sum of the count of distinct characters N where (1 <= N <= 105)in all the substrings. Examples: Input: str = "abbca"Output: 28Explanation: The following are the substrings o
8 min read
Count of substrings having all distinct characters
Given a string str consisting of lowercase alphabets, the task is to find the number of possible substrings (not necessarily distinct) that consists of distinct characters only.Examples: Input: Str = "gffg" Output: 6 Explanation: All possible substrings from the given string are, ( "g", "gf", "gff",
7 min read
Count the number of Unique Characters in a String in Python
We are given a string, and our task is to find the number of unique characters in it. For example, if the string is "hello world", the unique characters are {h, e, l, o, w, r, d}, so the output should be 8.Using setSet in Python is an unordered collection of unique elements automatically removing du
2 min read