“This is the 13th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021”

Title 1

859. Intimate strings

Given two strings s and goal, return true as long as we can get the same result as goal by swapping two letters in S; Otherwise return false.

Swap letters are defined by taking two subscripts I and j (subscripts starting at 0) that satisfy I! = j, then swap the characters at s[I] and s[j].

For example, swapping elements with subscripts 0 and 2 in “abcd” can produce “cbad”.

Example 1:

Input: s = “ab”, Goal = “ba” Output: true Explanation: You can swap s[0] = ‘a’ and S [1] = ‘b’ to produce “BA “, where S and goal are equal.

Example 2:

Explanation: You can only swap s[0] = ‘a’ and S [1] = ‘b’ to produce “BA “, where S and Goal are not equal.

Example 3:

Input: s = “AA “, Goal =” AA “Output: true Explanation: You can swap s[0] = ‘a’ and S [1] = ‘a’ to produce” AA “, where S and Goal are equal.

Example 4:

Input: s = “aaaaAAABC “, goal =” aaAAAAACb “Output: true

 

Tip:

1 <= S. length, goal.length <= 2 * 104 s and goal are lowercase letters

Analysis of the

  • stringsWith the stringgoalThe length is not equal, return directlyfalse
  • stringsWith the stringgoalIdentical strings are returned only if they are duplicated because of the swaptrue.
  • stringsWith the stringgoalNot equal: records characters in the same position and determines whether they swap equally

Code implementation

/ * * *@param {string} s
 * @param {string} goal
 * @return {boolean}* /
var buddyStrings = function(s, goal) {
    if(s.length ! = goal.length)return false
    if(s === goal) return s.length > new Set(s).size
    let a = ' '
    let b = ' '
    for(let i = 0; i<s.length; i++){if(s[i] ! == goal[i]){ a= s[i] + a b = b + goal[i] } }return a.length ===2 && a===b
};
Copy the code

Topic 2

Leetcode 860. Lemonade change

At the lemonade stand, a glass of lemonade costs $5. Customers line up to buy your product, one cup at a time (in bill bill order).

Each customer buys a glass of lemonade and pays you $5, $10 or $20. You have to give each customer the correct change, so the net transaction is that each customer pays you $5.

Notice that you don’t have any change to start with.

I’m going to give you an array of integers bills, where Bills [I] is the bill paid by the ith customer. Return true if you gave each customer the correct change, false otherwise.

 

Example 1:

Input: bills = [5,5,5,10,20] Output: true Explanation: We take three $5 bills from the first three customers, in order. At the fourth customer, we took a $10 bill and gave back $5. At the fifth customer, we gave back a $10 bill and a $5 bill. Since all customers got the correct change, we print true.

Example 2:

Input: bills = [5,5,10,10,20] Output: false Explanation: We take two $5 bills from the first two customers in order. For the next two customers, we charge a $10 bill and give $5 back. For the last customer, we can’t refund $15 because we only have two $10 bills. Since not every customer gets the correct change, the answer is false.

Example 3:

Input: bills = [5,5,10] output: true

Example 4:

Input: bills = [10,10] output: false

 

Tip:

1 <= bills. Length <= 105 bills[I] either 5, 10 or 20

Analysis of the

  • Define two variablesn5,n10This is the number of 5 yuan and the number of 10 yuan, and since 20 is not used for change, you don’t have to worry about 20
  • As you can see from the example, at firstN5 0.n10So whenbills[0]>5That means we can’t make changereturn false
  • traversebillsSo let’s say it’s $5n5++If it is 10 yuann5--,n10++, if it is 20 yuann5withn10Is both greater than 0 and if so,n5--.n10--;
  • Otherwise the judgen5Is it greater than equal 3 and if it is,n5 = n5-3
  • Otherwise,return false
  • Finally, we need guaranteesn5withn10It can’t be negative
  • When the traversal is complete, thenreturn true

Code implementation

/ * * *@param {number[]} bills
 * @return {boolean}* /
var lemonadeChange = function(bills) {
    if(bills[0] > 5) return false
    let n5 = 0
    let n10 = 0
    for(let i = 0; i<bills.length; i++){if(bills[i] ===5){
            n5++
        }
        if(bills[i] === 10){
            n5--
            n10++
        }
        if(bills[i] === 20) {if(n10 && n5){
                n5--
                n10--
            }else if(n5 >=3){
                n5 = n5-3
            }else{
                return false}}if(n10 < 0 || n5 < 0) return false
    }
    return true
};
Copy the code