Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6809463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:05:23+00:00 2026-05-26T20:05:23+00:00

I have Str A = abcdef Str B = abcdf I need a function(stA,

  • 0

I have

Str A = "abcdef"
Str B = "abcdf"

I need a function(stA, stB) that returns 5 (ie. the number of characters matched), note that these characters need to be in same order.

For example:

Str A = "abcdef"
Str B = "fedcba",

function(stA, stB) would only return 1 for the ‘a’

Pseudo code is good…

Oh btw given that all my strings will have <= 40 characters, O(n^2) might even be better than a O(41n) algorithm..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:05:24+00:00Added an answer on May 26, 2026 at 8:05 pm

    I haven’t fully tested it, but this seems to be working (in JavaScript) for your test cases:

    function compare(a, b)
    {
        var aChars = a.split('');
        var bChars = b.split('');
    
        var matches = [];
        var bStart = 0;
    
        for (var i=0; i < aChars.length; i++)
        {
            for (var j=bStart; j < bChars.length; j++)
            {
                if(aChars[i] == bChars[j])
                {
                    matches.push(aChars[i]);
                    bStart = j;
                    break;
                }
            }
        }
    
        return matches.length;
    }
    
    compare('abcdef', 'abcdf'); // returns 5
    compare('abcdef', 'fedcba'); // returns 1
    

    Basically I’m starting at position 0 for string A and position 0 for string B. When a match is found, I change the start position for searching string B so that it skips the previous section.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When we have a string that contains space characters: var str = ' A
If i have char* str; how do I write a function that accepts str
In Java I have: String str = Welcome 'thanks' How are you?; I need
In JavaScript I have a var str = .a long string that contains many
I have a const char* str with a very long string. I need to
I need to get a string out of the list of classes that have
I have: char *str = abc def abcdef ghi xyz; I want to assign
if i have a string str that is: a>b what is the best way
Let's suppose I have a string str which consists of characters default to hexadecimal
I made a function in C, that concatenates a variable number of strings. That's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.