The effective letter anagrams

Continue to create, accelerate growth! This is the sixth day of my participation in the "Nuggets Daily New Plan · October Update Challenge", click to view the details of the event

foreword

Before, Xiao Liuliu always felt that his algorithm was relatively bad, and it was a shortcoming. Before, he was really fishing for three days, drying the net for two days, and brushing for a few days, and then slowly he stopped insisting, so this Second, with the help of the platform's activities, I plan to start brushing slowly, and I will also summarize the brushing questions, talk about some of my own thinking, and my own ideas, etc. I hope it can be helpful to my friends. You can also take this opportunity to make up for your shortcomings. I hope you can stick to it.

collection of linked lists

topic

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

Example 1: Input: s = "anagram", t = "nagaram" Output: true

Example 2: Input: s = "rat", t = "car" Output: false

Explanation:  You can assume that the string contains only lowercase letters.

Example 1:

输入: s = "anagram", t = "nagaram"
输出: true
复制代码

Example 2:

输入: s = "rat", t = "car"
输出: false
复制代码

Violent solution, two-layer for loop

Well, I won't write this. The idea is to have two layers of for loops, and also record whether the characters are repeated. Obviously, the time complexity is O(n^2).

sort

t is an anagram of ss equivalent to "two strings sorted equal". Therefore, we can sort the strings ss and tt respectively, and judge whether the sorted strings are equal. Furthermore, if ss and tt are of different lengths, tt must not be an anagram of ss.

class Solution {
    public boolean isAnagram(String s, String t) {
        if (s.length() != t.length()) {
            return false;
        }
        char[] str1 = s.toCharArray();
        char[] str2 = t.toCharArray();
        Arrays.sort(str1);
        Arrays.sort(str2);
        return Arrays.equals(str1, str2);
    }
}
复制代码

hash table

From another point of view, tt is an anagram of ss, which is equivalent to "the kinds and number of characters in the two strings are equal". Since the string contains only 2626 lowercase letters, we can maintain a frequency array table with a length of 2626, first traverse the frequency of the characters in the record string ss, and then traverse the string tt, minus the corresponding frequency in the table, if If [i]<0table[i]<0, it means that tt contains an extra character that is not in ss, and you can return false.

class Solution {
    public boolean isAnagram(String s, String t) {
        if (s.length() != t.length()) {
            return false;
        }
        int[] table = new int[26];
        for (int i = 0; i < s.length(); i++) {
            table[s.charAt(i) - 'a']++;
        }
        for (int i = 0; i < t.length(); i++) {
            table[t.charAt(i) - 'a']--;
            if (table[t.charAt(i) - 'a'] < 0) {
                return false;
            }
        }
        return true;
    }
}
复制代码

image.png

Finish

Well, it's here today. I'm Xiao Liuliu. I fish for three days and net for two days!

Guess you like

Origin juejin.im/post/7150294939916566564