This is the 31st day of my participation in the August More Text Challenge


Related articles

LeetCode brush questions summary: LeetCode brush questions

1. Title Description


Looking for different

Given two strings s and t, they contain only lowercase letters.

The string t is randomly rearranged by the string S, and a letter is added at the random position.

Please find the letter added to t.

Start with simple questions to brush, exercise their thinking ability, for the interview preparation ~

Second, train of thought analysis


  • Look at the examples in the title, let’s clarify the idea ~

  • Example 1:

    Input: s = "abcd", t = "abcde" Output: "e" Explanation: 'e' is the letter being added.Copy the code
  • Example 2:

    Input: s = "", t = "y"Copy the code
  • Example 3:

    Input: s = "a", t = "aa" output: "a"Copy the code
  • Example 4:

    Input: s = "ae", t = "aea"Copy the code
  • Tip:

    0 <= s.length <= 1000 t.length == S.Length + 1 S and T contain only lowercase lettersCopy the code
  • Now the first reaction to this problem is to hash it out. But! I don’t have to!

  • Silently remembered another stupid way to compare.

  • Since the second character is only one more than the first character.

  • Then I replace all the characters of the first character with the second character, and the remaining character is the extra character.

  • Ha ha ha, mechanism like me.

AC code


  • Replacement crack:

    Class Solution {public char findTheDifference(String s, String t) {public char findTheDifference(String s, String t) {public char findTheDifference(String s, String t) { for (int i=0; i<strArr.length; I ++){// replaceFirst replaces only the first character t = t.replaceFirst(strArr[I],"") because there is a possibility that the second added string will be repeated; } return t.toCharArray()[0]; }}Copy the code
    • TSK TSK, although the efficiency can not bear to look at, but the operation is pretty SAO!
  • Bit operation cracking:

  • The above SAO operation efficiency is low, the following to solve the problem through xOR.

  • What? You can also use xor?

  • It turns out that t is just one more letter than s.

  • If we concatenate the two strings, the odd number of occurrences must be the extra character.

    class Solution { public char findTheDifference(String s, String t) { char res = 0; for (char c: s.toCharArray()) { res ^= c; } for (char c: t.toCharArray()) { res ^= c; } return res; }}Copy the code
    • Instant takeoff!

Four,

  • Thousands of ideas to solve the problem, whether this method is good, or whimsical solution, can solve is a good way! White cat black cat can catch mice cat is a good cat!
  • Here are a few other LeetCode solutions for reference!
  • Click to jump: official solution
  • Click jump: Draw solution algorithm

The road ahead is long, I see no end, I will search high and low

If you think I bloggers write well! Writing is not easy, please like, follow, comment and give encouragement to the blogger ~ Hahah