xxxxxxxxxx
yum__maintenance() {
command yum -y update
command yum -y upgrade
command yum clean all
}
yum__download() {
command yum -y install --downloadonly "$@"
}
yum() {
local cmdname=$1; shift
if type "yum__$cmdname" >/dev/null 2>&1; then
"yum__$cmdname" "$@"
else
command yum "$cmdname" "$@" # call the **real** yum command
fi
}
# if the functions above are sourced into an interactive interpreter, the user can
# just call "yum download" or "yum maintenance" with no further code needed.
# if invoked as a script rather than sourced, call function named on argv via the below;
# note that this must be the first operation other than a function definition
# for $_ to successfully distinguish between sourcing and invocation:
[[ $_ != $0 ]] && return
# make sure we actually *did* get passed a valid function name
if declare -f "$1" >/dev/null 2>&1; then
# invoke that function, passing arguments through
"$@" # same as "$1" "$2" "$3" ... for full argument list
else
echo "Function $1 not recognized" >&2
exit 1
fi
xxxxxxxxxx
/*
Javascript Solution - With Better Performance
Methods used in this algorithm are:
1. Two Pointer Technique
2. Sliding Window Technique
3. Hash Map
Time complexity: O(N)
Space complexity: O(N),
If the string does not contain repeated characters,
then the hash map will contain N characters.
*/
const s = "sdtvtsgh"
const lengthOfLongestSubstring = function(s) {
let max_length = 0
let map = {}
let l = 0
for (let r = 0; r < s.length; r++) {
if (map.hasOwnProperty(s[r])) {
const collision_index = map[s[r]]
if (collision_index >= l) {
l = collision_index + 1
}
delete map[s[collision_index]]
}
if (!map.hasOwnProperty(s[r])) {
map[s[r]] = r
}
if (max_length < (r + 1) - l) {
max_length = (r + 1) - l
}
console.log(map, ': (', r + 1, '-', l, ')')
}
return max_length
}
console.log(lengthOfLongestSubstring(s))
// [Log]:
{ s: 0 } : ( 1 - 0 )
{ s: 0, d: 1 } : ( 2 - 0 )
{ s: 0, d: 1, t: 2 } : ( 3 - 0 )
{ s: 0, d: 1, t: 2, v: 3 } : ( 4 - 0 )
{ s: 0, d: 1, v: 3, t: 4 } : ( 5 - 3 )
{ d: 1, v: 3, t: 4, s: 5 } : ( 6 - 3 )
{ d: 1, v: 3, t: 4, s: 5, g: 6 } : ( 7 - 3 )
{ d: 1, v: 3, t: 4, s: 5, g: 6, h: 7 } : ( 8 - 3 )
max_length = 5
xxxxxxxxxx
/**
* @param {string} s
* @return {number}
*/
var lengthOfLongestSubstring = function(s) {
if (!s.length) return 0
let left = 0, right = 0
let maxLength = -Infinity
const hashSet = {}
const len = s.length
while(right < len){
if (hashSet.hasOwnProperty(s[right])){
if (hashSet[s[right]] >= left){
left = hashSet[s[right]]+1
}
}
const lenn = right-left+1
maxLength = Math.max(lenn, maxLength)
hashSet[s[right]] = right
right++
}
return maxLength
};
xxxxxxxxxx
// JavaScript Solution - Credit to @albertchanged on LC
// Sliding window implementation
var lengthOfLongestSubstring = function(s) {
if (!s.length) return 0;
let left = 0, right = 0;
let maxLength = -Infinity;
const set = new Set();
while (right < s.length) {
// If s[right] has not been seen yet
if (!set.has(s[right])) {
// Add it to the set
set.add(s[right]);
// Increase size of window to right
right++;
// Update maxLength; set size represents length of unique substring
maxLength = Math.max(maxLength, set.size);
} else {
// We've seen s[right] so we need to shrink the window
// Delete s[left] from set
set.delete(s[left]);
// Shrink window from left
left++;
}
}
return maxLength;
}
xxxxxxxxxx
class Solution {
public int lengthOfLongestSubstring(String s) {int max=0;
HashMap<Character,Integer>hm=new HashMap<>();
for(int i=0,j=0;i<s.length();i++){
if(hm.containsKey(s.charAt(i))){
j=Math.max(j,hm.get(s.charAt(i))+1);
}
hm.put(s.charAt(i),i);
max=Math.max(max,i-j+1);
}
return max;
}
}
xxxxxxxxxx
public class Solution
{
public int LengthOfLongestSubstring(string str)
{
var dict = new Dictionary<char, int>();
var max = 0;
int start = 0;
for (int i = 0; i < str.Length; i++)
{
var x = str[i];
if (!dict.ContainsKey(x))
{
dict.Add(x, 1);
}
else
{
dict[x] += 1;
while (start <= i && dict.ContainsKey(str[start]) && dict[x] > 1)
{
dict[str[start]]--;
if (dict[str[start]] == 0)
dict.Remove(str[start]);
start++;
}
}
max = Math.Max(max, i - start + 1);
}
return max;
}
}
xxxxxxxxxx
/*
Given a string s, find the length of the longest substring (A substring is
a contiguous non-empty sequence of characters within a string.)
without repeating characters.
Example 1:
Input: s = "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.
Example 2:
Input: s = "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.
Example 3:
Input: s = "pwwkew"
Output: 3
Explanation: The answer is "wke", with the length of 3.
Notice that the answer must be a substring, "pwke" is a subsequence and
not a substring.
*/
const longestStr = s => {
if(s.length < 2) return s.length
let [left, right, partial, curIdx, finalLength] =
[0, s.length, new Set(), 0, -Infinity]
while (left < right) {
partial.add(s[left])
const curChar = s[left+1]
// Base case to exit loop early
if (finalLength > (right - curIdx)) break
if (partial.has(curChar)) {
curIdx += 1
left = curIdx
partial.size > finalLength ? finalLength = partial.size : null
partial.clear()
}else left++
left === right ? partial.size > finalLength ?
finalLength = partial.size : null : null
}
return finalLength
}
const s = "abcabcbb" // 3
const res = longestStr(s)
console.log(res)
/*
Time Complexity: O(n) (average case)
Space Complexity: O(n)
*/
// With love @kouqhar
xxxxxxxxxx
class Solution {
/**
* @param String $s
* @return Integer
*/
function lengthOfLongestSubstring($s) {
}
}