当前位置:网站首页>Repeated DNA sequence [hash determination repetition + sliding window + bit operation of binary coding]
Repeated DNA sequence [hash determination repetition + sliding window + bit operation of binary coding]
2022-07-27 20:58:00 【REN_ Linsen】
Binary bit operation
Preface
When determining the repeated numbers in the array , You can use hash Record ; Turn an array into a string , When determining whether the string is repeated , can hash Record the string in the sliding window ; But string hashcode To get, you need to scan the string , and Character/Integer Is to directly obtain the original value as hashcode(hashcode After interference, act as hash value .), So when string characters are binary encoded , Let the string map to a number , Get... Quickly hashcode.
One 、“ repeat ” Of DNA Sequence
Two 、hash Matching repetition + Binary code

1、hash Look for repetition
public List<String> findRepeatedDnaSequences(String s) {
int n = s.length();
Set<String> pool = new HashSet<>();
Set<String> ans = new HashSet<>();
for(int i = 0;i < n - 9;i++){
String str = s.substring(i,i + 10);
if(pool.contains(str) && !ans.contains(str)) ans.add(str);
pool.add(str);
}
return new ArrayList<>(ans);
}
2、 Bit operation practice of binary code
package everyday.bitManipulation;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
// Repetitive DNA Sequence .
public class FindRepeatedDnaSequences {
/* Because of the hashcode You need to scan the entire string , So you can encode a few strings , Just for 10 Encoding of substring . */
public List<String> findRepeatedDnaSequences(String s) {
int n = s.length();
List<String> ans = new ArrayList<>();
if (n <= L) return ans;
// Before initialization 9 Number .
int x = 0, i = 0;
while (i < L - 1) x = x << 2 | fx.get(s.charAt(i++));
// Start using the sliding window to get the substring integer Encoding as hash Of key
Map<Integer, Integer> cnt = new HashMap<>();
while (i < n) {
x = (x << 2 | fx.get(s.charAt(i))) & (1 << L * 2) - 1;
cnt.put(x, cnt.getOrDefault(x, 0) + 1);
if (cnt.get(x) == 2) ans.add(s.substring(i - L + 1, i + 1));
++i;
}
return ans;
}
final static int L = 10;
static Map<Character, Integer> fx = new HashMap<>();
static {
fx.put('A', 0);
fx.put('C', 1);
fx.put('G', 2);
fx.put('T', 3);
}
}
summary
1) Binary encoding can be more than string encoding , It is also common in subset scenarios , Binary mapping to subset ; Binary can also put 0/1 hash Dimension reduction , For example, a value can replace 31 position 0-1hash, deal with 26 Letters are more than enough .
2) Binary system / Bit operations are important , Practice more , It is helpful for many problem optimization .
3) A string of hashcode Acquisition is not O(1), It is O(n), character / The whole number is O(1).
reference
边栏推荐
- LabVIEW学习笔记五:按钮按下后无法返回原状
- User login switching case
- Vant component library
- 认识网络模型TCPIP模型
- 【R语言】【1】初学R语言语法使用Rstudio编辑
- 未定义变量 “Lattice“ 或类 “Lattice.latticeEasy“(Matlab)
- Arduino开发(二)_基于Arduino UNO开发板的RGB灯光控制方法
- 基于ATX自动化测试解决方案
- MySQL驱动jar包的下载--保姆教程
- Ue5 uses DLSS (super sampling) to improve the FPS of the scene away from the optimization scheme of Caton
猜你喜欢

Where is the program?

全局样式与图标

Best practices for Oracle kingbasees migration of Jincang database (4. Oracle database migration practice)

金仓数据库 Oracle 至 KingbaseES 迁移最佳实践 (4. Oracle数据库移植实战)

RK3399平台入门到精通系列讲解(导读篇)21天学习挑战介绍

Using dataX to realize efficient synchronization of MySQL data
![[Numpy] 数组属性](/img/eb/a27c24deeb7951828cdfbaa88c059c.png)
[Numpy] 数组属性

IOU 目标跟踪其一:IOU Tracker

Source Insight 4.0使用介绍

Automatic test solution based on ATX
随机推荐
UE5使用DLSS(超级采样)提升场景的 FPS 远离卡顿的优化方案
sql编码bug
金仓数据库 KingbaseES异构数据库移植指南 (2. 概述)
vi工作模式(3种)以及模式切换(转换)
R语言使用epiDisplay包的power.for.2p函数进行效用分析 ( 效能分析、Power analysis)、给定两个样本的比例值(proportions)、样本量计算效用值
R语言dplyr包summarise_at函数计算dataframe数据中多个数据列(通过向量指定)的计数个数、均值和中位数、使用list函数指定函数列表(使用.符号和~符号指定函数语法purr)
One article to understand pychar shortcut key
After working for bytek for two years, he got 15 offers at one go
MYSQL设计优化生成列
Programmer growth Chapter 18: project launch
js闭包知识
14天鸿蒙设备开发实战-第七章 设备联网上云 学习笔记
关于栈迁移的那些事儿
What configurations are required to connect polardb and redis?
未定义变量 “Lattice“ 或类 “Lattice.latticeEasy“(Matlab)
推荐一款强大的搜索工具Listary
js中数组与字符串常用方法属性总结
Download of MySQL driver jar package -- nanny tutorial
How does the industrial switch enter the web management interface?
Hexagon_V65_Programmers_Reference_Manual(8)