当前位置: 首页 > news >正文

LeetCode -- Substring with Concatenation of All Words

题目描述 :
You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concatenation of each word in words exactly once and without any intervening characters.


For example, given:
s: "barfoothefoobarman"
words: ["foo", "bar"]


You should return the indices: [0,9].
(order does not matter).


题目意思是说给定1个字符串s和一个单词数组words,个数为n,每个单词的长度都是l。
扫描s,判断在l * n范围内,判断是否words中的每个单词在s [i , l*n]内只出现了一次(如果words中包含1个单词多次,其中i∈[0,Len(s) - l*n)
注意,如果words本身包含重复的单词,那么这个单词也允许在s[i, l*n]中出现相同的次数




思路:
1. 使用哈希表1 :hash,遍历单词数组,存所有的单词以及出现的次数
2. 使用哈希表2 :hash2,在s中逐个截取长度为l的字符串str,如果str在hash1出现,添加到hash2中,其中l=s[i, words[j] * l]
3. 每次判断hash1中的元素是否完全等于hash2


实现代码:







public class Solution {
    public IList<int> FindSubstring(string s, string[] words) {
        
   	if(string.IsNullOrWhiteSpace(s) || words == null || s.Length < words.Length * words[0].Length){
		return new List<int>();
	}
	
	
	var wLen = words[0].Length;
	var result = new List<int>();
	var len = words.Length * wLen;
		
	var hash = new Dictionary<string, int>();
	for(var i = 0;i < words.Length; i++){
		if(!hash.ContainsKey(words[i])){
			hash.Add(words[i], 1);
		}
		else{
			hash[words[i]] ++;
		}
	}
	
	var h = new Dictionary<string, int>();
	for(var i = 0; i < s.Length - len + 1; i++){
		for(var j = 0;j < words.Length; j++){
			var str = s.Substring(i + j * wLen, wLen);
			if(hash.ContainsKey(str)){
				if(!h.ContainsKey(str)){
					h.Add(str,1);
				}
				else{
					h[str] ++;
				}
			}
		}
		
		if(hash.All(x=>h.ContainsKey(x.Key) && h[x.Key] == x.Value)){
			result.Add(i);
		}
		
		var k2 = new List<string>(h.Keys);
		foreach(var k in k2){
			h[k] = 0;
		}
	}
	
	
	return result;
    }
}


相关文章:

  • asp.net MVC5 sitemap 的使用
  • CentOS 5.x 預設啟動的服務簡易說明
  • Leet -- Remove Duplicates from Sorted Array
  • LeetCode -- Best Time to Buy and Sell Stock II
  • 海闊天空 信樂團
  • Contains Duplicate III
  • LeetCode -- Combination Sum
  • MySQL添加用户
  • LeetCode -- Candy
  • Leet -- Plus One
  • Leet -- Generate Parentheses
  • LeetCode -- Distinct Subsequences
  • LeetCode -- SpiralOrder
  • Windows 2003下成功配置IIS+Php+Mysql+Zend Optimizer+GD库+Phpmyadmin
  • LeetCode -- WordBreak II
  • [case10]使用RSQL实现端到端的动态查询
  • 《Java编程思想》读书笔记-对象导论
  • Bytom交易说明(账户管理模式)
  • gulp 教程
  • laravel 用artisan创建自己的模板
  • linux学习笔记
  • tensorflow学习笔记3——MNIST应用篇
  • 浮动相关
  • 基于OpenResty的Lua Web框架lor0.0.2预览版发布
  • 开发基于以太坊智能合约的DApp
  • 利用阿里云 OSS 搭建私有 Docker 仓库
  • 每个JavaScript开发人员应阅读的书【1】 - JavaScript: The Good Parts
  • 配置 PM2 实现代码自动发布
  • 推荐一个React的管理后台框架
  • 一些css基础学习笔记
  • ​MPV,汽车产品里一个特殊品类的进化过程
  • #define
  • #includecmath
  • #QT(智能家居界面-界面切换)
  • (AngularJS)Angular 控制器之间通信初探
  • (Matalb时序预测)PSO-BP粒子群算法优化BP神经网络的多维时序回归预测
  • (python)数据结构---字典
  • (SpringBoot)第七章:SpringBoot日志文件
  • (动手学习深度学习)第13章 计算机视觉---微调
  • (六)什么是Vite——热更新时vite、webpack做了什么
  • (算法)N皇后问题
  • (转)c++ std::pair 与 std::make
  • .gitignore文件---让git自动忽略指定文件
  • .NET “底层”异步编程模式——异步编程模型(Asynchronous Programming Model,APM)...
  • .net Application的目录
  • .net core webapi 大文件上传到wwwroot文件夹
  • .NET Core WebAPI中封装Swagger配置
  • .net framework profiles /.net framework 配置
  • .net 开发怎么实现前后端分离_前后端分离:分离式开发和一体式发布
  • .NET 药厂业务系统 CPU爆高分析
  • .netcore 如何获取系统中所有session_ASP.NET Core如何解决分布式Session一致性问题
  • .NET成年了,然后呢?
  • .Net转Java自学之路—SpringMVC框架篇六(异常处理)
  • /usr/lib/mysql/plugin权限_给数据库增加密码策略遇到的权限问题
  • [2019.2.28]BZOJ4033 [HAOI2015]树上染色