当前位置:网站首页>Strings in JS (including leetcode examples) < continuous update ~>
Strings in JS (including leetcode examples) < continuous update ~>
2022-06-12 18:03:00 【Out of the autistic bird】
List of articles
character string
String common methods
Inquire about
- Find position by string
Method name | explain |
---|---|
indexOf(‘ String to find ’, Starting position ) | Returns the position of the specified content in the original string , Go back if you can't find it -1, The starting position is index Reference no. |
lastIndexOf() | Look backwards , Just find the first match |
- Returns a string based on location
H5 Use str[index] that will do
operation
- Splicing
Method | explain |
---|---|
concat(str1,str2…) | Concatenate two or more strings , Equivalent to + |
- Intercept
Method | explain |
---|---|
substr(start,length) | from start Position start ( Reference no. ),length Take the number of , a key |
slice(start,end) | from start Start , Intercept to end Location , No end |
substring(start,end) | from start Start , Intercept to end, No end, Negative values are not acceptable |
- Replace
Method | explain |
---|---|
replace(‘ Replaced characters ’,‘ Replace character ’) | Replace only the first character |
replaceAll(‘ Replaced characters ’,‘ Replace character ’) | All replacement |
- Remove the space
Method | explain |
---|---|
trim() | Remove the leading and trailing spaces from the string |
- Convert case
Method | explain |
---|---|
toUpperCase() | Convert to uppercase |
toLowerCase() | Convert to lowercase |
String to array
- split(‘ Separator ’)
leetcode Example
387. The first unique character in the string
Relating to the number of occurrences , Don't hesitate. ,hash
// String traversal as key Deposit in map, Every time you meet one, give it to value+1
// Traversal string , Find the first map Of value by 1 Of , It's the first one that doesn't repeat
var firstUniqChar = function(s) {
let map = new Map()
for(let i=0;i<s.length;i++){
if(map.has(s[i])){
map.set(s[i],map.get(s[i])+1)
}else{
map.set(s[i],1)
}
}
for(let j=0;j<s.length;j++){
if(map.get(s.charAt(j)) == 1){
return j
}
}
return -1
};
383. Ransom letter
It is basically the same as the previous topic
// Use map, Traverse magazine,value Is a traversal element ,key Is the number of elements
// Traverse ransomNote, stay map And key Greater than 0, Description can match , to key-1, Otherwise failure
var canConstruct = function(ransomNote, magazine) {
const map = new Map()
for(let i=0;i<magazine.length;i++){
if(map.has(magazine[i])){
map.set(magazine[i],map.get(magazine[i])+1)
}else{
map.set(magazine[i],1)
}
}
for(let j=0;j<ransomNote.length;j++){
if(map.has(ransomNote[j]) && map.get(ransomNote[j])>0){
map.set(ransomNote[j],map.get(ransomNote[j])-1)
}else{
return false
}
}
return true
};
242. Effective alphabetic words
// Using arrays sort Sort Letters
var isAnagram = function(s, t) {
return [...s].sort().join('') === [...t].sort().join('')
};
// Same as above , Continue to use map
var isAnagram = function(s, t) {
if(s.length !== t.length){
return false
}
const map = new Map()
for(let i=0;i<s.length;i++){
if(map.has(s[i])){
map.set(s[i],map.get(s[i])+1)
}else{
map.set(s[i],1)
}
}
for(let j=0;j<t.length;j++){
if(map.has(t[j]) && map.get(t[j])>0){
map.set(t[j],map.get(t[j])-1)
}else{
return false
}
}
return true
};
边栏推荐
- Random talk about redis source code 91
- MySQL学习笔记
- Codeforces Round #398 (Div. 2) D. Cartons of milk
- "Big fat • small lesson" - talk about big file segmentation and breakpoint sequel
- JS中的栈(含leetcode例题)<持续更新~>
- PHP implementation of infinite classification tree (recursion and Optimization)
- 有关cookie的用法介绍
- Vant3+ts encapsulates uploader upload image component
- 干货 | 一文搞定 pytest 自动化测试框架(二)
- USB to serial port - maximum peak serial port baud rate vs maximum continuous communication baud rate
猜你喜欢
HTTP缓存<强缓存与协商缓存>
Applet and app are owned at the same time? A technical scheme with both
Esp-idf adds its own components
Advanced mountain -asp Net core router basic use demo 0.1
js求斐波那契数列
Nixos 22.05 installation process record
TypeScript类型声明文件(三)
利用小程序快速生成App,只需七步
USB to serial port - maximum peak serial port baud rate vs maximum continuous communication baud rate
ES7 does not use parent-child and nested relationships to implement one to many functions
随机推荐
重构--梳理并分解继承体系
Applet and app are owned at the same time? A technical scheme with both
JS中的栈(含leetcode例题)<持续更新~>
js判断回文数
C business serial number rule generation component
MIPS general purpose register + instruction
Leetcode491 increasing subsequence
USB转串口那些事儿—最大峰值串口波特率VS连续通信最高波特率
JS for Fibonacci sequence
机器学习系列(5):朴素贝叶斯
Database SQL operation Basics
Gospel of audio and video developers, rapid integration of AI dubbing capability
General differences between SQL server versions released by Microsoft in different periods so far, for reference
DRM driven MMAP detailed explanation: (I) preliminary knowledge
JS dichotomy
vant3+ts+pinia tab选项卡列表页面点击进详情,详情页返回tab高亮在原位置,刷新高亮默认在第一项
USB转串口那些事儿—串口驱动类型
用grep awk提取字符串
73. matrix zeroing (marking method)
vant3 +ts 封装简易step进步器组件