Skip to content

Instantly share code, notes, and snippets.

@Mustafa-Omran
Last active August 18, 2022 16:25
Show Gist options
  • Save Mustafa-Omran/91cb549f0a9905527845ceb723c7d852 to your computer and use it in GitHub Desktop.
Save Mustafa-Omran/91cb549f0a9905527845ceb723c7d852 to your computer and use it in GitHub Desktop.
Intersection of two Strings JS
private itersection(textOne: string, textTwo: string): string {
let textOneIndex: number = 0;
let textTwoIndex: number = 0;
let matchedText: string = '';
textOne = textOne.toLowerCase();
textTwo = textTwo.toLowerCase();
while (textOneIndex < textOne.length && textTwoIndex < textTwo.length) {
if (textOne[textOneIndex] < textTwo[textTwoIndex]) {
textOneIndex++;
} else if (textTwo[textTwoIndex] < textOne[textOneIndex]) {
textTwoIndex++;
} else {
matchedText = `${matchedText}${textTwo[textTwoIndex++]}`
textOneIndex++;
}
}
return matchedText;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment