首页 > 编程语言 >LeetCode-Java-893. Groups of Special-Equivalent Strings

LeetCode-Java-893. Groups of Special-Equivalent Strings

时间:2022-12-14 15:02:43浏览次数:50  
标签:893 Java int Equivalent equivalent groups Output Example special


题目

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.



Example 1:

Input: ["a","b","c","a","c","c"]
Output: 3
Explanation: 3 groups ["a","a"], ["b"], ["c","c","c"]
Example 2:

Input: ["aa","bb","ab","ba"]
Output: 4
Explanation: 4 groups ["aa"], ["bb"], ["ab"], ["ba"]
Example 3:

Input: ["abc","acb","bac","bca","cab","cba"]
Output: 3
Explanation: 3 groups ["abc","cba"], ["acb","bca"], ["bac","cab"]
Example 4:

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.

代码

主要是用一个数组进行标记,标记其偶数位和基数位各个字母的个数,最后进行比较。

class Solution {
public int numSpecialEquivGroups(String[] A) {
HashSet<String> set = new HashSet<String>();
int len = A.length;
for(int i=0;i<len;i++){
int[] a = new int[52];
String x = A[i];
int len1 = x.length();
for(int j=0;j<len1;j++){
a[x.charAt(j)-'a'+26*(j%2)]++;
}
set.add(Arrays.toString(a));

}
return set.size();
}

}


标签:893,Java,int,Equivalent,equivalent,groups,Output,Example,special
From: https://blog.51cto.com/u_12938555/5936994

相关文章

  • LeetCode-Java-575. Distribute Candies
    题目Givenanintegerarraywithevenlength,wheredifferentnumbersinthisarrayrepresentdifferentkindsofcandies.Eachnumbermeansonecandyofthecorr......
  • 能让Java开发者提高效率的10个工具
    ​ Java受到全球百万计开发者的追捧,已经演变为一门出色的编程语言。最终,这门语言随着技术的变化,不断的被改善以迎合变化的市场需求。无论你是否拥有一家科技公司,软件已经......
  • 剑指Offer-Java-重建二叉树
    题目输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍......
  • 剑指Offer-Java-序列化二叉树
    题目请实现两个函数,分别用来序列化和反序列化二叉树代码此题的核心点是如何表示二叉树,并且解释。/*publicclassTreeNode{intval=0;TreeNodeleft=null;......
  • Java做UI自动化和app自动化中动态代理@FindBy的工作原理【杭州多测师_王sir】【杭州多
    Java做UI自动化和app自动化中动态代理@FindBy的工作原理一、背景简介由于Selenium框架采用PageObject设计模式让测试代码与被测页面对象代码分离,因而提供了不少很方便的注......
  • 【都 Java19 了,还不了解 Java 8 ? 】一文带你深入了解 Java 8 新特性
    Java8(又称为jdk1.8)是Java语言开发的一个主要版本。Oracle公司于2014年3月18日发布Java8,它支持函数式编程,新的JavaScript引擎,新的日期API,新的Stream......
  • JAVA-枚举使用
    枚举在本教程中,我们将了解什么是Java枚举、它们解决的问题以及它们的一些设计模式如何在实践中使用。1.概述Java5首先引入了enum关键字。它表示一种特殊类型的类,它总......
  • 巨蟒python全栈开发数据库前端5:JavaScript1
     1.js介绍&变量&基础数据类型2.类型查询&运算符&if判断&for循环3.while循环&三元运算符4.函数5.今日总结 1.js介绍&变量&基础数据类型js介绍(1)什么是JavaScript&一些历史......
  • Java8新特性
    一、Java8新特性1.Lambda表达式Lambda是匿名函数,使用它可以写出简洁,灵活的代码。  a.表达式无参数,无返回值,只有一个Lambda体Runnable r1=()->log.info......
  • Java面向对象2
    封装性    封装就是保护内容,保证某些属性或方法可以不被外部看见,而在内部自己去处理。 classPerson{Stringname;intage;publicvoidtell(){System.out......