g0801_0900.s0893_groups_of_special_equivalent_strings.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java21 Show documentation
Show all versions of leetcode-in-java21 Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
package g0801_0900.s0893_groups_of_special_equivalent_strings;
// #Medium #Array #String #Hash_Table #2022_03_28_Time_3_ms_(99.09%)_Space_43.3_MB_(79.09%)
import java.util.HashSet;
/**
* 893 - Groups of Special-Equivalent Strings\.
*
* Medium
*
* You are given an array of strings of the same length `words`.
*
* In one **move** , you can swap any two even indexed characters or any two odd indexed characters of a string `words[i]`.
*
* Two strings `words[i]` and `words[j]` are **special-equivalent** if after any number of moves, `words[i] == words[j]`.
*
* * For example, `words[i] = "zzxy"` and `words[j] = "xyzz"` are **special-equivalent** because we may make the moves `"zzxy" -> "xzzy" -> "xyzz"`.
*
* A **group of special-equivalent strings** from `words` is a non-empty subset of words such that:
*
* * Every pair of strings in the group are special equivalent, and
* * The group is the largest size possible (i.e., there is not a string `words[i]` not in the group such that `words[i]` is special-equivalent to every string in the group).
*
* Return _the number of **groups of special-equivalent strings** from_ `words`.
*
* **Example 1:**
*
* **Input:** words = ["abcd","cdab","cbad","xyzz","zzxy","zzyx"]
*
* **Output:** 3
*
* **Explanation:**
*
* One group is ["abcd", "cdab", "cbad"], since they are all pairwise special equivalent, and none of the other
*
* strings is all pairwise special equivalent to these.
*
* The other two groups are ["xyzz", "zzxy"] and ["zzyx"].
*
* Note that in particular, "zzxy" is not special equivalent to "zzyx".
*
* **Example 2:**
*
* **Input:** words = ["abc","acb","bac","bca","cab","cba"]
*
* **Output:** 3
*
* **Constraints:**
*
* * `1 <= words.length <= 1000`
* * `1 <= words[i].length <= 20`
* * `words[i]` consist of lowercase English letters.
* * All the strings are of the same length.
**/
public class Solution {
public int numSpecialEquivGroups(String[] words) {
HashSet set = new HashSet<>();
int result = 0;
for (String str : words) {
if (set.add(getHashBySwap(str.toCharArray()))) {
result++;
}
}
return result;
}
private String getHashBySwap(char[] chars) {
for (int i = 0; i < chars.length; i++) {
int j = i + 2;
while (j < chars.length) {
if (chars[i] > chars[j]) {
char temp = chars[j];
chars[j] = chars[i];
chars[i] = temp;
}
j += 2;
}
}
return String.valueOf(chars);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy