From e6b5af60e34afbd134c5d1ed9a254fa1c7541bb8 Mon Sep 17 00:00:00 2001 From: marina <138340846+bt3gl-cryptographer@users.noreply.github.com> Date: Mon, 7 Aug 2023 17:38:41 -0700 Subject: [PATCH] Update README.md --- arrays_and_strings/README.md | 315 +++++++++++++++++++++++++++++++---- 1 file changed, 283 insertions(+), 32 deletions(-) diff --git a/arrays_and_strings/README.md b/arrays_and_strings/README.md index 318dc1b..ea6d1fc 100644 --- a/arrays_and_strings/README.md +++ b/arrays_and_strings/README.md @@ -2,24 +2,36 @@
-### comparing strings - -
- -* "==" can be used to compare two strings only if the language support operator overloading (like C++). +* arrays and strings hold values of the same type at contiguous memory location. arrays come with the advantage of storing multiple elements of the same type with one single variable name. + +* we are usually concernet with two things: the index of an element, and the element itself. + +* accessing elements is fast as long as you have the index (as opposed to linked lists that need to be traversed from the head). + +* addition or removal of elements into/from the middle of an array is slow because the remaining elements need to be shifted to accomodate the new/missing element (unless they are inserted/removed at the end of the list). + +* **subarray**: a range of contiguous values within an array (for example, in `[2, 3, 6, 1, 5, 4]`, `[3, 6, 1]` is a subarray while `[3, 1, 5]` is not). + +* **subsequence**: a sequence derived from the given sequence without changing the order (for example, in `[2, 3, 6, 1, 5, 4]`, `[3, 1, 5]` is a subsequence but `[3, 5, 1]` is not).
---- -### two-pointer technique +### two-pointer technique
-* a typical scenario is when you want to iterate the array from two ends to the middle. -* another secnario is when you need one slow-runner and one fast-runner at the same time (so that you can determine the movement strategy for both pointers). +* a typical scenario is when you want to iterate the array from two ends to the middle or when pointers can cross each others (or even be in different arrays). + +* another scenario is when you need one slow-runner and one fast-runner at the same time (so that you can determine the movement strategy for both pointers). + * in any case, this technique is usually used when the array is sorted. +* in the **sliding window** technique, the two pointers usually move in the same direction and never overtake each other. examples are: longest substring without repeating characters, minumum size subarray sum, minimum window substring. + + +
--- @@ -29,6 +41,7 @@
* in some languages (like C++), 2d arrays are represented as 1d, so an array of `m * n` elements represents `array[i][j]` as `array[i * n + j]`. + * dynamic 2d arrays a nested dynamic array. @@ -36,58 +49,296 @@ --- -### examples in this dir - -
- -#### `is_palindrome.py` +### check if mountain
```python -python3 is_palindrome.py - -Testing is_palindrome()... -Is subi no onibus a palindrone?: True -Is helllo there a palindrone?: False +def valid_mountain_array(arr: list[int]) -> bool: + + last_number, mountain_up = arr[0], True + + for i, n in enumerate(arr[1:]): + + if n > last_number: + if mountain_up == False: + return False + + elif n < last_number: + if i == 0: + return False + mountain_up = False + + else: + return False + + last_number = n + + return not mountain_up ```
-#### `playing_with_strings.py` +--- + +### duplicate zeros in place
```python -python3 playing_with_strings.py +def duplicate_zeros(arr: list[int]) -> list[int]: -Testing reverse_array_in_place -Array: [1, 2, 3, 4, 5] -Reversed: [5, 4, 3, 2, 1] + i = 0 + while i < len(arr): + + if arr[i] == 0 and i != len(arr) - 1: + + range_here = len(arr) - (i + 2) + while range_here > 0: + arr[i + range_here + 1] = arr[i + range_here] + range_here -= 1 + + arr[i+1] = 0 + i += 2 + + else: + i += 1 + + return arr ```
-#### `anagram.py` +---- + +### remove duplicates in place + +
+ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# author: bt3gl + + +```python +def remove_duplicates(nums: list[int]) -> int: + + arr_i, dup_i = 0, 1 + + while arr_i < len(nums) and dup_i < len(nums): + + if nums[arr_i] == nums[dup_i]: + dup_i += 1 + + else: + arr_i += 1 + nums[arr_i] = nums[dup_i] + + for i in range(arr_i + 1, dup_i): + nums[i] = '_' + + return dup_i - arr_i - 1, nums +``` + +
+ +---- + +### check if permutation is palindrome
```python -python3 anagram.py +def is_permutation_of_palindromes(some_string): -Testing is_anagram()... -Is listen an anagram of silent?: True + aux_dict = {} + + for c in some_string.strip(): + + if c in aux_dict.keys(): + aux_dict[c] -= 1 + else: + aux_dict[c] = 1 + + for v in aux_dict.values(): + if v != 0: + return False + + return True ``` +
-#### `permutation.py` +--- + +### intersection of two arrays
```python -python3 permutation.py - -Testing permutation()... -Permutation of bt3gl: ['bt3gl', 'bt3lg', 'btg3l', 'btgl3', 'btl3g', 'btlg3', 'b3tgl', 'b3tlg', 'b3gtl', 'b3glt', 'b3ltg', 'b3lgt', 'bgt3l', 'bgtl3', 'bg3tl', 'bg3lt', 'bglt3', 'bgl3t', 'blt3g', 'bltg3', 'bl3tg', 'bl3gt', 'blgt3', 'blg3t', 'tb3gl', 'tb3lg', 'tbg3l', 'tbgl3', 'tbl3g', 'tblg3', 't3bgl', 't3blg', 't3gbl', 't3glb', 't3lbg', 't3lgb', 'tgb3l', 'tgbl3', 'tg3bl', 'tg3lb', 'tglb3', 'tgl3b', 'tlb3g', 'tlbg3', 'tl3bg', 'tl3gb', 'tlgb3', 'tlg3b', '3btgl', '3btlg', '3bgtl', '3bglt', '3bltg', '3blgt', '3tbgl', '3tblg', '3tgbl', '3tglb', '3tlbg', '3tlgb', '3gbtl', '3gblt', '3gtbl', '3gtlb', '3glbt', '3gltb', '3lbtg', '3lbgt', '3ltbg', '3ltgb', '3lgbt', '3lgtb', 'gbt3l', 'gbtl3', 'gb3tl', 'gb3lt', 'gblt3', 'gbl3t', 'gtb3l', 'gtbl3', 'gt3bl', 'gt3lb', 'gtlb3', 'gtl3b', 'g3btl', 'g3blt', 'g3tbl', 'g3tlb', 'g3lbt', 'g3ltb', 'glbt3', 'glb3t', 'gltb3', 'glt3b', 'gl3bt', 'gl3tb', 'lbt3g', 'lbtg3', 'lb3tg', 'lb3gt', 'lbgt3', 'lbg3t', 'ltb3g', 'ltbg3', 'lt3bg', 'lt3gb', 'ltgb3', 'ltg3b', 'l3btg', 'l3bgt', 'l3tbg', 'l3tgb', 'l3gbt', 'l3gtb', 'lgbt3', 'lgb3t', 'lgtb3', 'lgt3b', 'lg3bt', 'lg3tb'] +def intersect(nums1: list[int], nums2: list[int]) -> list[int]: + + result = [] + set_nums = set(nums1) & set(nums2) + counter = Counter(nums1) & Counter(nums2) + + for n in set_nums: + result.extend([n] * counter[n]) + + return result ``` + +
+ +### check if anagram + +```python +def is_anagram(string1, string2) -> bool: + + string1 = string1.lower() + string2 = string2.lower() + + if len(string1) != len(string2): + return False + + for c in string1: + if c not in string2: + return False + + return True +``` + +
+ +--- + +### check if isomorphic + +
+ +```python +def is_isomorphic(s: str, t: str) -> bool: + + map_s_to_t = {} + map_t_to_s = {} + + for ss, tt in zip(s, t): + + if (ss not in map_s_to_t) and (tt not in map_t_to_s): + map_s_to_t[ss] = tt + map_t_to_s[tt] = ss + + elif (map_s_to_t.get(ss) != tt) or (map_t_to_s.get(tt) != ss): + return False + + return True +``` + +
+ +--- + +### absolute difference between the sums of a matrix's diagonals + +
+ +```python + +def diagonal_difference(arr): + + diag_1 = 0 + diag_2 = 0 + + i, j = 0, len(arr) - 1 + + while i < len(arr) and j >= 0: + + diag_1 += arr[i][i] + diag_2 += arr[i][j] + i += 1 + j -= 1 + + return diag_1, diag_2, abs(diag_1 - diag_2) +``` + +
+ +### is palindrome + +
+ +```python +def is_palindrome(sentence): + + sentence = sentence.strip(' ') + if len(sentence) < 2: + return True + + if sentence[0] == sentence[-1]: + return is_palindrome(sentence[1:-1]) + + return False +``` + +
+ +--- + +### find permutations + +
+ +```python + + +def permutations(string) -> list: + + if len(string) == 1: + return [string] + + result = [] + for i, char in enumerate(string): + for perm in permutation(string[:i] + string[i+1:]): + result += [char + perm] + + return result +``` + +
+ +--- + +### length of the longest substring + +
+ +```python +def length_longest_substring(s) -> int: + + result = "" + this_longest_string = "" + i = 0 + + for c in s: + j = 0 + + while j < len(this_longest_string): + + if c == this_longest_string[j]: + if len(this_longest_string) > len(result): + result = this_longest_string + this_longest_string = this_longest_string[j+1:] + + j += 1 + + this_longest_string += c + + return result, this_longest_string +``` + +
+ +--- + +###