Snippets Collections
int[] arr = {1,2,3,4,5,6};
          for(int i =0; i<arr.length; i++){
            System.out.println("key "  + arr[i]);
        }
        
        ----------------
        ArrayList<Integer> arr = new ArrayList<>();
        arr.add(1);
        arr.add(3);
         arr.add(5);
          arr.add(6);
        
        for(int i =0; i<arr.size(); i++){
            System.out.println("key "  + arr.get(i));
        }


--------------
Map<String, Integer> map = new HashMap<String, Integer>();
      
      map.put("key", 11);
      map.put("key1", 11);
      map.put("key2", 11);
      map.put("key3", 15);
      map.put("key4", 11);
      map.put("key5", 11);
      map.put("key6", 11);
      
      for(Map.Entry<String, Integer> entry : map.entrySet()){
          String key = entry.getKey();
          int value = entry.getValue();
          
          System.out.println(" "+ key + " "+ value);
      }

---------------------------
  
def length_of_longest_substring(arr, k):
  #set start, maxLength, maxRepeat 
  start, maxLength, maxRepeat = 0, 0, 0 
  #set frequencyDict 
  frequencyDict = {} 
  
  #iterate through arr
  for end in range(len(arr)): 
    #add to frequencyDict 
    right = arr[end]
    if right not in frequencyDict: 
        frequencyDict[right] = 1 
    else: 
        frequencyDict[right] += 1 
    #find maxRepeat 
    maxRepeat = max(maxRepeat, frequencyDict[right]) 
    
    #evaluate window 
    if (end - start + 1 - maxRepeat) > k: 
        #reduce window by 1 and adjust frequency map 
        left = arr[start] 
        frequencyDict[left] -= 1 
        start += 1 
    
    #check maxLength 
    maxLength = max(maxLength, end - start + 1) 

  return maxLength

length_of_longest_substring([0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1], 3)

class Solution:
    def checkInclusion(self, s1: str, s2: str) -> bool:
        s1Dict = {}
    
        for char in s1: 
            if char not in s1Dict: 
                s1Dict[char] = 1 
            else: 
                s1Dict[char] += 1 

        left = 0 


        compareDict = {}

        for right in range((left + (len(s1) - 1)), len(s2)): 
            if s2[left] in s1: 
                compare = s2[left:right + 1] #makes a list of chars from left to right pointers 
                for char in compare: 
                    if char not in compareDict: 
                        compareDict[char] = 1 
                    else: 
                        compareDict[char] += 1 
                if compareDict == s1Dict: 
                    return True 
                else: 
                    compareDict.clear() 
                    left += 1 
                    continue 



            else: 
                left += 1 

        return False 
star

Sat Apr 13 2024 10:42:39 GMT+0000 (Coordinated Universal Time) Gigame Portal

##array #hashmap
star

Thu Sep 22 2022 20:42:29 GMT+0000 (Coordinated Universal Time) https://leetcode.com/problems/permutation-in-string/

#python #neetcode #hashmap

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension