All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0801_0900.s0893_groups_of_special_equivalent_strings.Solution.kt Maven / Gradle / Ivy

There is a newer version: 1.30
Show newest version
package g0801_0900.s0893_groups_of_special_equivalent_strings

// #Medium #Array #String #Hash_Table #2023_04_11_Time_141_ms_(100.00%)_Space_34.7_MB_(100.00%)

class Solution {
    fun numSpecialEquivGroups(words: Array): Int {
        val set: HashSet = HashSet()
        var result = 0
        for (str in words) {
            if (set.add(getHashBySwap(str.toCharArray()))) {
                result++
            }
        }
        return result
    }

    private fun getHashBySwap(chars: CharArray): String {
        for (i in chars.indices) {
            var j = i + 2
            while (j < chars.size) {
                if (chars[i] > chars[j]) {
                    val temp = chars[j]
                    chars[j] = chars[i]
                    chars[i] = temp
                }
                j += 2
            }
        }
        return String(chars)
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy