📌  相关文章
📜  恰好执行一次交换后可以获得的不同字符串的计数

📅  最后修改于: 2022-05-13 01:57:08.768000             🧑  作者: Mango

恰好执行一次交换后可以获得的不同字符串的计数

给定一个包含小写英文字母字符的字符串s 。任务是计算在恰好执行一次交换后可以获得的不同字符串的数量。

方法:这个问题可以通过使用HashMaps来解决。请按照以下步骤解决给定的问题。

  • 检查字符串s中唯一元素的数量。
  • 将所有唯一字符的频率存储在地图中。
  • 声明一个变量ans=0来存储不同的可能字符串的数量。
  • 遍历字符串并增加ans的除当前元素之外的元素,该元素将用于构造一个新的字符串。
  • 再次遍历字符串并检查某些字符的频率是否大于 2。如果是,则将 answer 增加 1,因为它们只会形成一个额外的唯一字符串。
  • 返回ans作为最终答案。

下面是上述方法的实现

C++
// C++ program for above approach
#include 
using namespace std;
 
// Function to count number of distinct
// string formed after one swap
long long countStrings(string S)
{
    long long N = S.size();
 
    // mp[] to store the frequency
    // of each character
    int mp[26] = { 0 };
 
    // For storing frequencies
    for (auto i : S) {
        mp[i - 'a']++;
    }
    long long ans = 0;
    for (auto i : S) {
        ans += N - mp[i - 'a'];
    }
    ans /= 2;
 
    for (int i = 0; i < 26; i++) {
        if (mp[i] >= 2) {
            ans++;
            break;
        }
    }
    return ans;
}
 
// Driver Code
int main()
{
    string S = "geek";
 
    // Function Call
    long long ans = countStrings(S);
    cout << ans << endl;
 
    return 0;
}


Java
// Java code to implement the above approach
import java.util.*;
public class GFG
{
   
// Function to count number of distinct
// string formed after one swap
static long countStrings(String S)
{
    long N = S.length();
 
    // mp[] to store the frequency
    // of each character
    int mp[] = new int[26];
    for(int i = 0; i < 26; i++) {
        mp[i] = 0;
    }
 
    // For storing frequencies
    for (int i = 0; i < S.length(); i++) {
        mp[S.charAt(i) - 'a']++;
    }
    long ans = 0;
    for (int i = 0; i < S.length(); i++) {
        ans += N - mp[S.charAt(i) - 'a'];
    }
    ans /= 2;
 
    for (int i = 0; i < 26; i++) {
        if (mp[i] >= 2) {
            ans++;
            break;
        }
    }
    return ans;
}
 
// Driver code
public static void main(String args[])
{
    String S = "geek";
 
    // Function Call
    long ans = countStrings(S);
    System.out.println(ans);
}
}
 
// This code is contributed by Samim Hossain Mondal.


Python3
# Python code to implement the above approach
 
# Function to count number of distinct
# string formed after one swap
def countStrings(S):
    N = len(S);
 
    # mp to store the frequency
    # of each character
    mp = [0 for i in range(26)];
    for i in range(26):
        mp[i] = 0;
 
    # For storing frequencies
    for i in range(N):
        mp[ord(S[i]) - ord('a')] += 1;
 
    ans = 0;
    for i in range(N):
        ans += N - mp[ord(S[i]) - ord('a')];
 
    ans //= 2;
 
    for i in range(26):
        if (mp[i] >= 2):
            ans += 1;
            break;
 
    return ans;
 
# Driver code
if __name__ == '__main__':
    S = "geek";
 
    # Function Call
    ans = countStrings(S);
    print(ans);
 
# This code is contributed by 29AjayKumar


C#
// C# code to implement the above approach
using System;
class GFG
{
   
// Function to count number of distinct
// string formed after one swap
static long countStrings(string S)
{
    long N = S.Length;
 
    // mp[] to store the frequency
    // of each character
    int []mp = new int[26];
    for(int i = 0; i < 26; i++) {
        mp[i] = 0;
    }
 
    // For storing frequencies
    for (int i = 0; i < S.Length; i++) {
        mp[S[i] - 'a']++;
    }
    long ans = 0;
    for (int i = 0; i < S.Length; i++) {
        ans += N - mp[S[i] - 'a'];
    }
    ans /= 2;
 
    for (int i = 0; i < 26; i++) {
        if (mp[i] >= 2) {
            ans++;
            break;
        }
    }
    return ans;
}
 
// Driver code
public static void Main()
{
    string S = "geek";
 
    // Function Call
    long ans = countStrings(S);
    Console.Write(ans);
}
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript


输出
6

时间复杂度: 在)
辅助空间: O(1)