当前位置:网站首页>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

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
};
原网站

版权声明
本文为[Out of the autistic bird]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/163/202206121758170527.html