Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,638 questions

55,373 answers

573 users

How to check if two equal-length strings are at least 50% equal in TypeScript

1 Answer

0 votes
function are50PercentEqual(str1: string, str2: string) : boolean {
    if (str1 === null || str2 === null || str1.length !== str2.length) {
        return false;
    }
 
    let matchingChars: number = 0;
 
    for (let i: number = 0; i < str1.length; i++) {
        if (str1[i] === str2[i]) {
            matchingChars++;
        }
    }
 
    return (matchingChars / str1.length) >= 0.5;
}
 
 
const str1: string = "typescript c# c++ c python";
const str2: string = "typescript c# r c rust sql";
 
if (are50PercentEqual(str1, str2)) {
    console.log("yes");
} else {
    console.log("no");
}
 
 
 
/*
run:
 
"yes" 

*/

 



answered May 11, 2024 by avibootz

Related questions

...