[case of algorithm thousand questions] practice LeetCode punch in once a day - 103 Intimate string

Posted by bolerophone on Mon, 24 Jan 2022 00:55:10 +0100

📢 preface

🚀 Algorithm problem 🚀
  • 🌲 Punching out an algorithm problem every day is both a learning process and a sharing process 😜
  • 🌲 Tip: the problem-solving programming languages in this column are C# and Java
  • 🌲 To maintain a state of learning every day, let's work together to become the great God of algorithm 🧐!
  • 🌲 Today is the 103rd day of punching out the force deduction algorithm 🎈!
🚀 Algorithm problem 🚀

🌲 Sample of original question: intimate string

Give you two strings S and goal. As long as we can get the result equal to goal by exchanging the two letters in s, we will return true; Otherwise, false is returned.

The definition of exchange letter is: take two subscripts I and J (subscript starts from 0) and meet I= j. The characters at s[i] and s[j] are then exchanged.

For example, exchanging elements of subscript 0 and subscript 2 in "abcd" can generate "cbad".

Example 1:

Input: s = "ab", goal = "ba"
Output: true
 Explanation: you can exchange s[0] = 'a' and s[1] = 'b' generate "ba",here s and goal equal.

Example 2:

Input: s = "ab", goal = "ab"
Output: false
 Explanation: you can only exchange s[0] = 'a' and s[1] = 'b' generate "ba",here s and goal Unequal.

Example 3:

Input: s = "aa", goal = "aa"
Output: true
 Explanation: you can exchange s[0] = 'a' and s[1] = 'a' generate "aa",here s and goal equal.

Example 4:

Input: s = "aaaaaaabc", goal = "aaaaaaacb"
Output: true

Tips:

  • 1 <= s.length, goal.length <= 2 * 104
  • s and goal are composed of lowercase English letters

🌻 C# method: Enumeration

Let the two strings be s and goal, where s[i] represents the ith character of S, and goal[i] represents the ith character of goal.

If s[i]=goal[i], we say I is matched, otherwise I is unmatched.

Intimate string is defined as: the ith character s[i] in s needs to be exchanged with the jth character in s, and i =j is satisfied. After exchange, s is equal to goal.

Two strings of intimate strings need to be exchanged once, and two characters with unequal indexes are equal.

code:

public class Solution {
    public bool BuddyStrings(string s, string goal) {
        if (s.Length != goal.Length) {
            return false;
        }
        
        if (s.Equals(goal)) {
            int[] count = new int[26];
            for (int i = 0; i < s.Length; i++) {
                count[s[i] - 'a']++;
                if (count[s[i] - 'a'] > 1) {
                    return true;
                }
            }
            return false;
        } else {
            int first = -1, second = -1;
            for (int i = 0; i < goal.Length; i++) {
                if (s[i] != goal[i]) {
                    if (first == -1)
                        first = i;
                    else if (second == -1)
                        second = i;
                    else
                        return false;
                }
            }

            return (second != -1 && s[first] == goal[second] && s[second] == goal[first]);
        }
    }
}

results of enforcement

adopt
 Execution time: 84 ms,At all C# Defeated 47.14% of users in submission
 Memory consumption: 37.9 MB,At all C# Defeated 23.70% of users in submission

🌻 Java methods: enumerations

Train of thought analysis

Let the two strings be s and goal respectively, where s[i] represents the ith character of S and goal[i] represents the ith character of goal.

If s[i]=goal[i], we say I is matched, otherwise I is unmatched.

Intimate string is defined as: the ith character s[i] in s needs to be exchanged with the jth character in s, and i =j is satisfied. After exchange, s is equal to goal.

Two strings of intimate strings need to be exchanged once, and two characters with unequal indexes are equal.

code:

class Solution {
    public boolean buddyStrings(String s, String goal) {
        if (s.length() != goal.length()) {
            return false;
        }
        
        if (s.equals(goal)) {
            int[] count = new int[26];
            for (int i = 0; i < s.length(); i++) {
                count[s.charAt(i) - 'a']++;
                if (count[s.charAt(i) - 'a'] > 1) {
                    return true;
                }
            }
            return false;
        } else {
            int first = -1, second = -1;
            for (int i = 0; i < goal.length(); i++) {
                if (s.charAt(i) != goal.charAt(i)) {
                    if (first == -1)
                        first = i;
                    else if (second == -1)
                        second = i;
                    else
                        return false;
                }
            }

            return (second != -1 && s.charAt(first) == goal.charAt(second) &&
                    s.charAt(second) == goal.charAt(first));
        }
    }
}

results of enforcement

adopt
 Execution time: 2 ms,At all Java  Defeated 67 in submission.41%User
 Memory consumption: 38.4 MB,At all Java Defeated 15 in submission.53%User

Complexity analysis

Time complexity: O(N),among N Is the length of the string
 Space complexity: O(C),You need a constant space to save the character statistics of the string. We count the number of all lowercase letters, so C = 26. 

💬 summary

  • Today is the 103rd day of punching out the force deduction algorithm!
  • This paper uses C# and Java programming languages to solve problems
  • Some methods are also written by Likou God, and they are also shared while learning. Thanks again to the algorithm bosses
  • That's the end of today's algorithm sharing. See you tomorrow!

Topics: Java Algorithm leetcode