Skip to content

Instantly share code, notes, and snippets.

@nikonov91-dev
Last active May 5, 2022 16:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nikonov91-dev/aa080576018035a51ce9f02ea31ac976 to your computer and use it in GitHub Desktop.
Save nikonov91-dev/aa080576018035a51ce9f02ea31ac976 to your computer and use it in GitHub Desktop.
// 1 цель: надо собрать перебрать подстроки по макс.
// длине подстроки с неважно каким порядком, главное не алфавитное сортировка
// а максимально подборка неповторимых букв с возможным повторение друг за другом
const ss1 = 'ab';
const ss2 = 'cba';
const ss3 = 'ccabb';
const temp = 'abccbbacaaaba';
type indexes = {
[k:string]:number
};
function func(str:string) {
const uniqueLetters: Set<string> = new Set(str);
const subCandidates:Array<string> = [];
const strArr = str.split('');
const indexes = {};
const tempo:number[][] = [];
for (let u of uniqueLetters) {
tempo.push(strArr.reduce((acc:number[],n,i) => i === 0 ? [0] : (n === u ? [...acc, i] : acc), []))
}
};
function findSubstrings(uniqueLetters: Set<string>, strs: Array<string>) {
const indexes: Array<string>[] = [];
let arrangedLetters: string[] = [];
let count: number = 0;
let i: number = 0;
let j: number = 0;
console.log('before count');
const compare2Strings = (p: string, n: string): string => {
let finalString: string = '';
const pSet = new Set(p);
const nSet = new Set(n);
if (pSet.size === uniqueLetters.size && nSet.size < uniqueLetters.size) {
console.log('pSet === uniqueLetters')
finalString = p;
} else if (pSet.size < uniqueLetters.size && nSet.size === uniqueLetters.size) {
console.log('nSet === uniqueLetters')
finalString = n;
} else if (pSet.size === uniqueLetters.size && nSet.size === uniqueLetters.size) {
console.log('pSet === nSet')
const isStringValid = (pp: string): boolean => {
const ppArray = pp.split('');
const ppSet = new Set(ppArray);
const allLetters:Array<boolean> = [];
const checkAllLetters = (arr: Array<string>, l: string): boolean => {
const occurencies: Array<number> = arr.reduce((accum: Array<number>, c: string, ii: number) =>
ii === 0 ? [0] : ( c === l ? [...accum, ii] : accum), []);//[0,2,3]
const b: Array<boolean> = occurencies.reduce((accum: Array<boolean>, c: number, ii: number) => {
return ii === 0 ? [true] : (occurencies[ii] - 1 === occurencies[ii - 1] ? [...accum, true] : [...accum, false])
}, []);//[true,false,true]
return b.every(e => !!e);
}
for (const l of ppSet) {
allLetters.push(checkAllLetters(ppArray, l));
}
return allLetters.every(e=>!!e)
};
if (isStringValid(p))
finalString = p;
else
finalString = n;
} else
finalString = 'no string found';
return finalString
}
const a = strs.reduce(compare2Strings);
console.log('final string: ' + a)
};
findSubstrings(temp, [ss1, ss2, ss3]);
console.log('end')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment