[daily force deduction 15] effective letter words

Posted by sebjlan on Tue, 04 Jan 2022 02:50:46 +0100

1, Title [LeetCode-242]

Given two strings s and t, write a function to determine whether t is an alphabetic ectopic word of s.

Note: if each character in , s and t , occurs the same number of times, then , s and t , are called alphabetic words.

Example 1:

Input: s = "anagram", t = "nagaram"

Output: true

Example 2:

Input: s = "rat", t = "car"

Output: false

Tips:

1 <= s.length, t.length <= 5 * 10^4

s and t # contain lowercase letters only

Advanced: what if the input string contains unicode characters? Can you adjust your solution to deal with this situation?

2, Train of thought

Method 1: hash map method

First, judge whether the character length is the same. If not, return false directly.

Like day14, you can create a hash table unordered_ Map < char, int > to record each character in the string and the number of occurrences of each character. Here, create hash tables for s and t, respectively.

The first loop traverses s and T, and converts the characters and corresponding times to sCount[s[i]] + + and tCount[t[i]] +. In the second loop, judge whether each character s[i] in S has the same number of times in S and t. sCount[s[i]]==tCount[s[i]]. If there are different characters, return false. If all characters are the same, return true.

class Solution {
public:
    bool isAnagram(string s, string t) {
        unordered_map<char, int> sCount;
        unordered_map<char, int> tCount;
        int n = s.size();
        if(n != t.size())
            return false;
        for(int i = 0; i < n; i++)
        {
            sCount[s[i]]++;
            tCount[t[i]]++;
        }
        for(int i = 0; i < n; i++)
        {
            if(sCount[s[i]]!=tCount[s[i]])
                return false;
        }
        return true;
    }
};

 

Method 2: frequency array (equivalent to the hash table whose hash function is hash(x)=x, and the array value record count)

This question limits the contents of s and t to lowercase letters. There are only 26 cases, which can be realized by the simplest array.

The process is the same as method 1, but it is implemented in the form of array. The difference between the ASCII code of these lowercase letters and the ASCII code of 'a' needs to be used as the keyword (index of the array). In the second round of 26 cycles (26 lowercase letters starting from a), judge whether sCount[i] is equal to tCount[i] (representing whether the times of the corresponding letter 'a' + I in the s and t arrays are the same). If not, return false. Return true at the end of the loop.

class Solution {
public:
    bool isAnagram(string s, string t) {
        int n = s.size();
        if(n != t.size())
            return false;
        int sCount[26] = {0};
        int tCount[26] = {0};//Declare the frequency array and initialize to 0
        for(int i = 0; i < n; i++)
        {
            int sIndex = s[i]-'a';
            int tIndex = t[i]-'a';
            sCount[sIndex]++;
            tCount[tIndex]++;
        }
        for(int i = 0; i < 26; i++)
        {
            if(sCount[i]!=tCount[i])
                return false;
        }
        return true;
    }
};

For the advanced problem, since the frequency array is limited to 26, if you want to expand, you need to declare more array space, but the loading factor may be very small and there is too much space waste. Therefore, method 1 is suitable for the advanced problem.

3, Official solution

Method 1: sorting

T is an ectopic word of s, which is equivalent to "two strings are equal after sorting". Therefore, we can sort the strings s and t respectively to see whether the sorted strings are equal. In addition, if the lengths of s and T are different, t must not be an ectopic word of s.

class Solution {
public:
    bool isAnagram(string s, string t) {
        if (s.length() != t.length()) {
            return false;
        }
        sort(s.begin(), s.end());
        sort(t.begin(), t.end());
        return s == t;
    }
};

Author: LeetCode-Solution
 Link: https://leetcode-cn.com/problems/valid-anagram/solution/you-xiao-de-zi-mu-yi-wei-ci-by-leetcode-solution/
Source: force buckle( LeetCode)
The copyright belongs to the author. For commercial reprint, please contact the author for authorization, and for non-commercial reprint, please indicate the source.

Complexity analysis

  • Time complexity: O(nlogn), where n is the length of s. The time complexity of sorting is O(nlogn), and the time complexity of comparing whether the two strings are equal is O(n). Therefore, the overall time complexity is O(nlogn+n)=O(nlogn).
  • Spatial complexity: O(logn). Sorting requires O(logn) spatial complexity.

Method 2: hash table

From another point of view, the ectopic word t is s is equivalent to "the types and times of characters in two strings are equal". Since the string only contains 26 lowercase letters, we can maintain a frequency array table with a length of 26. First traverse the frequency of characters in the record string s, and then traverse the string t, minus the corresponding frequency in the table. If table [i] < 0 occurs, it indicates that t contains an additional character not in s, and return false.

class Solution {
public:
    bool isAnagram(string s, string t) {
        if (s.length() != t.length()) {
            return false;
        }
        vector<int> table(26, 0);
        for (auto& ch: s) {
            table[ch - 'a']++;
        }
        for (auto& ch: t) {
            table[ch - 'a']--;
            if (table[ch - 'a'] < 0) {
                return false;
            }
        }
        return true;
    }
};

Author: LeetCode-Solution
 Link: https://leetcode-cn.com/problems/valid-anagram/solution/you-xiao-de-zi-mu-yi-wei-ci-by-leetcode-solution/
Source: force buckle( LeetCode)
The copyright belongs to the author. For commercial reprint, please contact the author for authorization, and for non-commercial reprint, please indicate the source.

Complexity analysis

  • Time complexity: O(n), where n is the length of s.
  • Space complexity: O(S), where S is the character set size, where S=26.

4, Learning experience

The concept of frequency array: an array used to record the keyword count. The array index is the keyword (hash(x) = x), and the array value is the keyword occurrence. Frequency array makes use of the advantages of simple array implementation, easy index (O (1) time) and hash table constant time o (1) insertion, deletion and search. It can play a great role when it comes to the number of elements in data structures such as array, list word and character string.

In addition, the essence of bucket sorting is also a frequency array, and cardinal sorting is equivalent to multi round bucket sorting. Sorting can be realized in O (1) time, which is often used for a group of elements in the key value comparison set.

Topics: C++ Algorithm leetcode string