javascript
javascript有效的anagram挑战
In today’s blog post, I will build an algorithm that validates given inputs as anagrams. Here is one of the most classic interview challenge named Valid Anagram which I picked from LeetCode’s Top Interview Questions List:
在今天的博客文章中,我將構建一種算法來驗證給定輸入的字謎。 這是我從LeetCode的“熱門面試問題列表”中選出的最經典的面試挑戰之一,稱為“有效Anagram” :
Given two strings s and t, write a function to determine if t is an anagram of s (You may assume the string contains only lowercase alphabets).
給定兩個字符串s和t ,編寫一個函數以確定t是否是s的字謎(您可以假設該字符串僅包含小寫字母)。
Words that are anagrams are formed from another by rearranging its letters, so they will have the same letters with the same frequency in a different order. “listen” and “silent” can be given as an example to a common anagram. For this challenge, we need to determine if given strings are valid anagrams of each other. Let’s see a couple of examples to better understand the question:
anagram s的單詞是通過重新排列字母而由另一個單詞形成的,因此它們將以相同的頻率以不同的順序具有相同的字母。 “聽”和“沉默”可以作為常見字謎的一個例子。 對于這個挑戰,我們需要確定給定的字符串是否彼此有效。 讓我們看幾個例子來更好地理解這個問題:
Example 1:Input: s = "anagram", t = "nagaram"Output: trueExample 2:Input: s = "rat", t = "car"
Output: false
To solve a problem, there are always multiple approaches and solutions with varying runtimes. Sorting the characters of both strings and then comparing them one by one can be the first thing that comes to our mind to solve this given problem, but remember that time takes to sort is larger than O(n). My approach will be to use a hashmap as our data structure and we will not need to use inbuilt methods of JavaScript in this way:
為了解決問題,總有多種方法和解決方案具有不同的運行時間。 對兩個字符串的字符進行排序,然后一一比較它們可能是解決這個給定問題的第一件事,但是請記住,排序所需的時間大于O(n)。 我的方法是將哈希圖用作我們的數據結構,并且我們不需要以這種方式使用JavaScript的內置方法:
Compare the lengths of the two given strings and return false if they are not equal.
比較兩個給定字符串的長度,如果它們不相等,則返回false 。
Loop over the second string and check if the character is in the object created above. If not, return false. If the character exists in the hash, decrement the value for that character by 1.
循環遍歷第二個字符串,并檢查字符是否在上面創建的對象中。 如果不是,則返回false 。 如果哈希中存在該字符,則將該字符的值減1。
After both loops are done, return true, because this means that both the lengths and characters of the strings are equal.
在完成兩個循環之后,返回true ,因為這意味著字符串的長度和字符都相等。
Let’s see a simple implementation of the above logic in JavaScript:
讓我們看一下上述邏輯在JavaScript中的簡單實現:
An anagram of a word can be created by rearranging the letters of the word using each letter only once. isAnagram function above will compare two strings to see if they contain the same characters the same number of times. We first check if both strings are of the same length, if not; no comparison is needed. Then create a hashmap and iterate over one of the strings to determine the number of times each character appeared. This step stores the characters of the string as keys and their appearance as values. In the second iteration with for…of loop, check if the characters of the second string already exist in the object. If it does, subtract one from its value. If it doesn’t, immediately return false because the strings are not anagrams. Return true if both loops complete, it means that the lengths and characters of the strings are analyzed and we can conclude that the strings are anagrams.
可以通過僅使用每個字母一次重新排列單詞的字母來創建單詞的字謎。 上面的isAnagram函數將比較兩個字符串,以查看它們是否包含相同字符相同的次數。 我們首先檢查兩個字符串的長度是否相同(如果不是); 無需比較。 然后創建一個哈希表并遍歷字符串之一,以確定每個字符出現的次數。 此步驟將字符串的字符存儲為鍵,并將其外觀存儲為值。 在for…of循環的第二次迭代for…of ,檢查第二個字符串的字符是否已存在于對象中。 如果是這樣,請從其值中減去一個。 如果不是,則立即返回false因為字符串不是字謎。 如果兩個循環都完成,則返回true ,這意味著將分析字符串的長度和字符,我們可以得出結論,這些字符串是字謎。
Here’s what it looks like without comments:
這是沒有注釋的樣子:
For our approach, we used two iterations (one per string) and provided a solution in linear time, so the time complexity is O(n), where n is the length of the string. Note that we are looping over both strings only once.
對于我們的方法,我們使用了兩次迭代(每個字符串一個),并提供了線性時間的解決方案,因此時間復雜度為O(n) ,其中n是字符串的長度。 請注意,我們僅循環兩個字符串一次。
Thanks for reading, I hope you enjoyed solving this question! Here are my other articles on different algorithms solved in JavaScript if you are interested:
感謝您的閱讀,希望您喜歡解決這個問題! 如果您有興趣,這是我的其他文章,介紹了用JavaScript解決的不同算法:
翻譯自: https://medium.com/javascript-in-plain-english/javascript-valid-anagram-challenge-70bc346fdf4c
總結
以上是生活随笔為你收集整理的javascript有效的anagram挑战的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 写在最初
- 下一篇: 解决gif 透明度问题