Skip to main content

Arrays & Hashing:- Valid Anagram | Two Sum | Group Anagrams

Valid Anagram

Given two strings s and t, return true if t is an anagram of s, and false otherwise.

Example 1:

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

Output: true

Example 2:

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

Output: false


Solution:

class Solution(object):
    def isAnagram(self, s, t):
        """
        :type s: str
        :type t: str
        :rtype: bool
        """
        if len(s)!= len(t):
            return False
       
        countS, countT= {},{}

        for i in range(len(s)):
            countS[s[i]]= 1 + countS.get(s[i], 0)
            countT[t[i]]= 1 + countT.get(t[i], 0)
       
        return countS == countT
       

Two Sum

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

You can return the answer in any order.

Example 1:

Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].

Example 2:

Input: nums = [3,2,4], target = 6
Output: [1,2]

Solution:

class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:

indexMap={} # index : value , i:n

for i, n in enumerate(nums): # if nums = [2,7,11,15], target = 9
diff:[int]= target - n # 9 - 7
if diff in indexMap: # 2 already added to hashmap
return [indexMap[diff], i] # [0,1] : [2,7]
indexMap[n] = i #[2:0]
#[7:1]

The enumerate() function in Python is  built-in tool that simplifies looping returning it as an enumerate object which produces pairs of (index, value).

a = ["Python", "Java", "C++"]

for i, v in enumerate(a):

    print(i, v)

Group Anagrams

Given an array of strings strs, group the  together. You can return the answer in any order.

Example 1:

Input: strs = ["eat","tea","tan","ate","nat","bat"]

Output: [["bat"],["nat","tan"],["ate","eat","tea"]]

Explanation:

  • There is no string in strs that can be rearranged to form "bat".
  • The strings "nat" and "tan" are anagrams as they can be rearranged to form each other.
  • The strings "ate", "eat", and "tea" are anagrams as they can be rearranged to form each other.

Example 2:

Input: strs = [""]

Output: [[""]]

Example 3:

Input: strs = ["a"]

Output: [["a"]]

Solution:

class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
res=defaultdict(list)# hashmap/dictionary
for s in strs:
count=[0]*26
for c in s:
count[ord(c)- ord("a")] +=1
res[tuple(count)].append(s)

return list(res.values())

Check out my Cyber Security Youtube channel: zodiac

Comments

Popular posts from this blog

Bug Boundy Methodology, Tools & Resources

Start by defining a clear objective, such as exploiting a remote code execution (RCE) vulnerability or bypassing authentication on your target. Then, consider how you can achieve this goal using various attack vectors like XSS, SSRF, or others - these are simply tools to help you reach your objective. Use the target as how a normal user would, while browsing keep these questions in mind: 1)How does the app pass data? 2)How/where does the app talk about users? 3)Does the app have multi-tenancy or user levels? 4)Does the app have a unique threat model? 5)Has there been past security research & vulnerabilities? 6)How does the app handle XSS, CSRF, and code injection?

Install & set up mitmweb or mitmproxy in Linux

Step 1: Go to the mitmproxy page and download the binaries. Step 2: Install the downloaded tar file with the command " tar -xzf <filename>.tar.gz " Step 3: In the FoxyProxy add the proxy 127.0.0.1:8080  and turn it on. Step 4 : In the terminal run command " ./mitmweb " Step 5: Go to the page  http://mitm.it/   and download the mitmproxy's Certificate. Step 6: If you downloaded the certificate for Firefox, then go to " settings -> Privacy & Security -> Click View Certificates -> Click  Import ", then import the certificate.  Step 7: Now you are ready to capture the web traffic. Step 8 : In terminal run " ./mitmweb"

Install & setup Genymotion for APK Dynamic Analysis in MobSF in Linux

Download Genymotion   .bin file Type command:  chmod +x filename.bin                             ./filename.bin Create an account in Genymotion website. Log in to the the App. Click the "+" button on the top and add a device(phone model). Run this command in terminal: "docker run -it --rm \ -p 8000:8000 \ -p 1337:1337 \ -e MOBSF_ANALYZER_IDENTIFIER=<adb device identifier> \ opensecurity/mobile-security-framework-mobsf:latest" In the <adb device identifier>  add your value at his  place   docker run -it --rm \ -p 8000:8000 \ -p 1337:1337 \ -e MOBSF_ANALYZER_IDENTIFIER=192.168.58.102:5555 \ opensecurity/mobile-security-framework-mobsf:latest Press enter.