当前位置:首页|资讯

GFG 30 Exactly one swap

作者:您是打尖儿还是住店呢发布时间:2024-10-03


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.

In one swap,Geek can pick two distinct index i and j (i.e 1 < i < j < |S| ) of the string, then swap the characters at the position i and j.

Example 1:

Input:

S = "geek"

Output:

6

Explanation:

After one swap, There are only 6 distinct strings possible.(i.e "egek","eegk","geek","geke","gkee" and "keeg")

Example 2:


Input:

S = "ab"

Output:

1

Explanation:

Only one string is possible after one swap(i.e "ba")


Your Task:

You don't need to read input or print anything. Complete the function countStrings( ) which takes the string S as input parameters and returns the required answer.


Constraints:

2 ≤ |S| ≤ 10

S contains lowercase characters

-----------


给定一个包含小写英文字母的字符串 S。任务是计算进行一次精确交换后可以得到的不同字符串的数量。


在一次交换中,Geek 可以选取字符串中两个不同的索引 i 和 j(即 1 < i < j < |S| ),然后交换 i 和 j 位置上的字符。


示例 1:


输入:


S = "geek


输出: 6


6


说明:


经过一次交换后,可能只有 6 个不同的字符串(即 “egek”、“eegk”、“geek”、“geke”、“gekee ”和 “keeg”)。


例 2:




输入


S = "ab


输出: 1


1


说明: 交换一次后只能得到一个字符串(即 “ba”):


交换一次后只能得到一个字符串(即 "ba)




你的任务


您不需要读取输入或打印任何内容。完成以字符串 S 为输入参数的函数 countStrings( ) 并返回所需的答案。




限制条件


2 ≤ |S| ≤ 10


S 包含小写字符

----

hashmap即可。



Copyright © 2024 aigcdaily.cn  北京智识时代科技有限公司  版权所有  京ICP备2023006237号-1