30. Substring with Concatenation of All Words

复制代码
package LeetCode_30

/**
 * 30. Substring with Concatenation of All Words
 * https://leetcode.com/problems/substring-with-concatenation-of-all-words/
 *
 * You are given a string s and an array of strings words of the same length.
 * Return all starting indices of substring(s) in s that is a concatenation of each word in words exactly once,
 * in any order, and without any intervening characters. You can return the answer in any order.

Example 1:
Input: s = "barfoothefoobarman", words = ["foo","bar"]
Output: [0,9]
Explanation: Substrings starting at index 0 and 9 are "barfoo" and "foobar" respectively.
The output order does not matter, returning [9,0] is fine too.

Example 2:
Input: s = "wordgoodgoodgoodbestword", words = ["word","good","best","word"]
Output: []

Example 3:
Input: s = "barfoofoobarthefoobarman", words = ["bar","foo","the"]
Output: [6,9,12]

Constraints:
1. 1 <= s.length <= 104
2. s consists of lower-case English letters.
3. 1 <= words.length <= 5000
4. 1 <= words[i].length <= 30
5. words[i] consists of lower-case English letters.
 * */
class Solution {
    /*
    * solution: HashMap, Time complexity:O(n*l), Space complexity:O(n), l is length of s, n is count of word
    * */
    fun findSubstring(s: String, words: Array<String>): List<Int> {
        if (s == null || s.isEmpty() || words == null || words.isEmpty()) {
            return ArrayList()
        }
        val frequencyMap = HashMap<String, Int>()
        for (word in words) {
            frequencyMap.put(word, frequencyMap.getOrDefault(word, 0) + 1)
        }
        val result = ArrayList<Int>()
        val wordLength = words[0].length
        val totalWordCount = words.size
        var i = 0
        //while (i <= s.length - wordLength * totalWordCount) {
        while (i < s.length) {
            val seedWord = HashMap<String, Int>()
            for (j in 0 until totalWordCount) {
                //check each word one by one
                val startIndex = i + j * wordLength
                var endIndex = startIndex + wordLength
                if (endIndex > s.lastIndex) {
                    endIndex = s.lastIndex+1
                }
                val curWord = s.substring(startIndex, endIndex)
                if (!frequencyMap.contains(curWord)) {
                    break
                }
                seedWord.put(curWord, seedWord.getOrDefault(curWord, 0) + 1)
                println(seedWord)
                if (seedWord.get(curWord) ?: 0 > frequencyMap.getOrDefault(curWord, 0)) {
                    //handle case: capcapmap,cap's frequency in frequencyMap is 1, so just need the second 'cap'
                    break
                }
                //if j can go through the length of word
                if (j + 1 == totalWordCount) {
                    result.add(i)
                }
            }
            i++
        }
        return result
    }
}
复制代码

 

posted @   johnny_zhao  阅读(66)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2019-10-26 74. Search a 2D Matrix
2019-10-26 743. Network Delay Time
点击右上角即可分享
微信分享提示