|
| 1 | +package com.fishercoder.solutions; |
| 2 | + |
| 3 | +import java.util.Arrays; |
| 4 | +import java.util.HashMap; |
| 5 | +import java.util.HashSet; |
| 6 | +import java.util.Map; |
| 7 | +import java.util.Set; |
| 8 | +import java.util.stream.Collectors; |
| 9 | + |
| 10 | +/** |
| 11 | + * 893. Groups of Special-Equivalent Strings |
| 12 | + * |
| 13 | + * You are given an array A of strings. |
| 14 | + * A move onto S consists of swapping any two even indexed characters of S, or any two odd indexed characters of S. |
| 15 | + * Two strings S and T are special-equivalent if after any number of moves onto S, S == T. |
| 16 | + * For example, S = "zzxy" and T = "xyzz" are special-equivalent because we may make the moves "zzxy" -> "xzzy" -> "xyzz" |
| 17 | + * that swap S[0] and S[2], then S[1] and S[3]. |
| 18 | + * |
| 19 | + * Now, a group of special-equivalent strings from A is a non-empty subset of A such that: |
| 20 | + * Every pair of strings in the group are special equivalent, and; |
| 21 | + * The group is the largest size possible (ie., there isn't a string S not in the group such that S is special equivalent to every string in the group) |
| 22 | + * Return the number of groups of special-equivalent strings from A. |
| 23 | + * |
| 24 | + * Example 1: |
| 25 | + * Input: ["abcd","cdab","cbad","xyzz","zzxy","zzyx"] |
| 26 | + * Output: 3 |
| 27 | + * Explanation: |
| 28 | + * One group is ["abcd", "cdab", "cbad"], since they are all pairwise special equivalent, and none of the other strings are all pairwise special equivalent to these. |
| 29 | + * The other two groups are ["xyzz", "zzxy"] and ["zzyx"]. Note that in particular, "zzxy" is not special equivalent to "zzyx". |
| 30 | + * |
| 31 | + * Example 2: |
| 32 | + * Input: ["abc","acb","bac","bca","cab","cba"] |
| 33 | + * Output: 3 |
| 34 | + * |
| 35 | + * Note: |
| 36 | + * 1 <= A.length <= 1000 |
| 37 | + * 1 <= A[i].length <= 20 |
| 38 | + * All A[i] have the same length. |
| 39 | + * All A[i] consist of only lowercase letters. |
| 40 | + * */ |
| 41 | +public class _893 { |
| 42 | + public static class Solution1 { |
| 43 | + /**my original solution, a bit length: |
| 44 | + * generate a unique signaure as key for each equivelant group and sum them up*/ |
| 45 | + public int numSpecialEquivGroups(String[] A) { |
| 46 | + return Arrays.stream(A).map(this::getCommonKey).collect(Collectors.toSet()).size(); |
| 47 | + } |
| 48 | + |
| 49 | + private String getCommonKey(String word) { |
| 50 | + char[] oddIndexed = new char[word.length() / 2]; |
| 51 | + char[] evenIndexed = new char[word.length() / 2 + (word.length() % 2 == 0 ? 0 : 1)]; |
| 52 | + char[] array = word.toCharArray(); |
| 53 | + for (int i = 0; i < array.length - 1; i += 2) { |
| 54 | + evenIndexed[i / 2] = array[i]; |
| 55 | + oddIndexed[i / 2] = array[i + 1]; |
| 56 | + } |
| 57 | + if (word.length() % 2 != 0) { |
| 58 | + evenIndexed[evenIndexed.length - 1] = array[array.length - 1]; |
| 59 | + } |
| 60 | + Arrays.sort(oddIndexed); |
| 61 | + Arrays.sort(evenIndexed); |
| 62 | + return new StringBuffer().append(new String(evenIndexed)).append(new String(oddIndexed)).toString(); |
| 63 | + } |
| 64 | + } |
| 65 | +} |
0 commit comments