C#LeetCode刷题之#893-特殊等价字符串组​​​​​​​​​​​​​​(Groups of Special-Equivalent Strings)

C#LeetCode刷题之#893-特殊等价字符串组​​​​​​​​​​​​​​(Groups of Special-Equivalent Strings)

问题

你将得到一个字符串数组 A。

如果经过任意次数的移动,S == T,那么两个字符串 S 和 T 是特殊等价的。

一次移动包括选择两个索引 i 和 j,且 i%2 == j%2,并且交换 S[j] 和 S [i]。

现在规定,A 中的特殊等价字符串组是 A 的非空子集 S,这样不在 S 中的任何字符串与 S 中的任何字符串都不是特殊等价的。

返回 A 中特殊等价字符串组的数量。

输入:[“a”,”b”,”c”,”a”,”c”,”c”]

输出:3

解释:3 组 [“a”,”a”],[“b”],[“c”,”c”,”c”]

输入:[“aa”,”bb”,”ab”,”ba”]

输出:4

解释:4 组 [“aa”],[“bb”],[“ab”],[“ba”]

输入:[“abc”,”acb”,”bac”,”bca”,”cab”,”cba”]

输出:3

解释:3 组 [“abc”,”cba”],[“acb”,”bca”],[“bac”,”cab”]

输入:[“abcd”,”cdab”,”adcb”,”cbad”]

输出:1

解释:1 组 [“abcd”,”cdab”,”adcb”,”cbad”]

提示:

  • 1 <= A.length <= 1000
  • 1 <= A[i].length <= 20
  • 所有 A[i] 都具有相同的长度。
  • 所有 A[i] 都只由小写字母组成。

You are given an array A of strings.

Two strings S and T are special-equivalent if after any number of moves, S == T.

A move consists of choosing two indices i and j with i % 2 == j % 2, and swapping S[i] with S[j].

Now, a group of special-equivalent strings from A is a non-empty subset S of A such that any string not in S is not special-equivalent with any string in S.

Return the number of groups of special-equivalent strings from A.

Input: [“a”,”b”,”c”,”a”,”c”,”c”]

Output: 3

Explanation: 3 groups [“a”,”a”], [“b”], [“c”,”c”,”c”]

Input: [“aa”,”bb”,”ab”,”ba”]

Output: 4

Explanation: 4 groups [“aa”], [“bb”], [“ab”], [“ba”]

Input: [“abc”,”acb”,”bac”,”bca”,”cab”,”cba”]

Output: 3

Explanation: 3 groups [“abc”,”cba”], [“acb”,”bca”], [“bac”,”cab”]

Input: [“abcd”,”cdab”,”adcb”,”cbad”]

Output: 1

Explanation: 1 group [“abcd”,”cdab”,”adcb”,”cbad”]

Note:

  • 1 <= A.length <= 1000
  • 1 <= A[i].length <= 20
  • All A[i] have the same length.
  • All A[i] consist of only lowercase letters.

示例

public class Program {

    public static void Main(string[] args) {
        var S = "I speak Goat Latin";

        var res = ToGoatLatin(S);
        Console.WriteLine(res);

        Console.ReadKey();
    }

    private static string ToGoatLatin(string S) {
        //按空格分隔
        var split = S.Split(' ');
        //定义结果
        var res = string.Empty;
        for(var i = 0; i < split.Length; i++) {
            if(IsStartsWithVowel(split[i])) {
                res += split[i];
            } else {
                //辅音字母开头时,首字母后置
                res += split[i].Substring(1) + split[i][0];
            }
            //追回字符串 ma 和按索引重复的字符 a
            res += "ma" + RepeatString(i + 1) + " ";
        }
        return res.Trim();
    }

    private static bool IsStartsWithVowel(string word) {
        //判断是不是元音字母
        var s = word[0].ToString().ToLower();
        return s == "a" || s == "e" || s == "i" || s == "o" || s == "u";
    }

    private static string RepeatString(int count) {
        //重复字符串
        var c = 'a';
        var res = string.Empty;
        for(var i = 0; i < count; i++) {
            res += c;
        }
        return res;
    }

}

以上给出1种算法实现,以下是这个案例的输出结果:

Imaa peaksmaaa oatGmaaaa atinLmaaaaa

分析:

显而易见,以上算法的时间复杂度为: O(n)​ 。

本文由 .Net中文网 原创发布,欢迎大家踊跃转载。

转载请注明本文地址:https://www.byteflying.com/archives/3975

(1)
上一篇 2020年06月08日 21:04
下一篇 2020年06月08日 21:09

相关推荐

发表回复

登录后才能评论