最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Compare two string and count letters that are different - Stack Overflow

programmeradmin5浏览0评论

I have to write a function that takes in two strings and pares them then returns the number of letters in the strings that are different. For example "ABC" and "DEC" should return 4. My function always turns up one short because of how I am paring them. I've looked but can't seem to find a fix.

I have tried looping through the string without splitting and ended up with the same problem.

function makeAnagram(a, b) {
    let result = 0;

    let a1 = a.split("").sort();
    let b1 = b.split("").sort();
    for(let i = 0; i < b1.length; i++){
        if(a1.indexOf(b1[i] < 0)){
            result += 1;
        }
    }


    return result;
 }

I have to write a function that takes in two strings and pares them then returns the number of letters in the strings that are different. For example "ABC" and "DEC" should return 4. My function always turns up one short because of how I am paring them. I've looked but can't seem to find a fix.

I have tried looping through the string without splitting and ended up with the same problem.

function makeAnagram(a, b) {
    let result = 0;

    let a1 = a.split("").sort();
    let b1 = b.split("").sort();
    for(let i = 0; i < b1.length; i++){
        if(a1.indexOf(b1[i] < 0)){
            result += 1;
        }
    }


    return result;
 }
Share Improve this question edited Nov 6, 2019 at 9:31 Gowri Pranith Kumar 1,6852 gold badges12 silver badges22 bronze badges asked Nov 6, 2019 at 9:21 TEbogoTEbogo 2292 gold badges3 silver badges12 bronze badges 7
  • 1 Your closing bracket is in the wrong place... a1.indexOf(b1[i] < 0) should be a1.indexOf(b1[i]) < 0. Also, there's no need to do the .sort() – freefaller Commented Nov 6, 2019 at 9:25
  • 1 how the heck "ABC" and "DEC" should return 4 when there are 3 letters in each word.... – Gibor Commented Nov 6, 2019 at 9:25
  • @Gibor - because they're after different letters – freefaller Commented Nov 6, 2019 at 9:26
  • @gibor they have C in mon, so the difference is ABDE, which is 4 long. – Matt Ellen Commented Nov 6, 2019 at 9:26
  • 1 May I know what would be your result if ABCC and DEC ? Same 4 or 5? – Narandhran Thangavel Commented Nov 6, 2019 at 9:34
 |  Show 2 more ments

4 Answers 4

Reset to default 5

In one line:

("ABC"+"DEC").split('').sort().join('').replace(/(.)\1+/g, "").length

Returns

4

Steps of the program:

  1. ("ABC"+"DEC") makes a string with the 2 merged words : ABCDEC

  2. ("ABC"+"DEC").split('').sort().join('') makes the characters sorted in the string: ABCCDE. This will enable us to find duplicates easily with regex

  3. replace(/(.)\1+/g, "") removes all sequences of 2+ characters, then we get ABDE

  4. .length counts the remaining characters, which are the ones with single occurence.

You can do:

Edited as suggestion by @freefaller

const makeAnagram = (a, b) => {
  const arr1 = a.split('')
  const arr2 = b.split('')
  const diff1 = arr1.filter(letter => !arr2.includes(letter))
  const diff2 = arr2.filter(letter => !arr1.includes(letter))
  
  return diff1.length + diff2.length
}

console.log(makeAnagram('ABC', 'DEC'))

An ES6 way to do the same

const makeAnagram = (a, b) => new Set(a + b).size - new Set([...a].filter(x => b.includes(x))).size;
console.log(makeAnagram('ABC', 'DEC')); // prints 4

This is what I would do

Implementation

let makeAnagram = (a,b) => {
  let clubStr = ('' + a).concat(b);
  let sortedStr = clubStr.trim().split('').sort().join('');
  let unmonStr = sortedStr.replace(/(\w)\1+/gi, '');
  return unmonStr.length;
};

You can do same in one liner.

Caller : makeAnagram('ABC', 'DCE')

Ouput : 4

发布评论

评论列表(0)

  1. 暂无评论