简体   繁体   中英

How to check two string have same characters including special characters

I have two question

1) how can I check two shuffle string have same characters Like I have

var str1 = "ansar@#//1";
var str2 = "@#//sanra1";

should return true

2) If not have same characters then which are the characters not exist in second sting

var str1 = "ansar@#//123";
var str2 = "@#//sanra";

Should return 123

I know I can get first question answer by

str1.length===str2.length && str1.split("").sort().join() == str2.split("").sort().join(); 

But not sure how to get characters not matched with second string

Consider these implementations:

1)

var str1 = "ansar@#//1";
var str2 = "@#//sanra1";

function first(str1, str2) {
    return Array.prototype.every.call(str1, function(c) {
        return str2.indexOf(c) > -1;
    }, this);
}

console.log(first(str1, str2));
var str1 = "ansar@#//123";
var str2 = "@#//sanra";

function second() {
    return Array.prototype.filter.call(str1, function(c) {
        return str2.indexOf(c) === -1;
    }, this).join('');
}

console.log(second(str1, str2));

Update for checking if strings contain same characters you can use ES6 Set:

const checkIfStringsContainSameCharacters = (string1, string2) => {
  return new Set(string1).size === new Set(string1 + string2).size;
}

using a while loop seems a reasonable solution:

 var str1 = "ansar@#//1"; var str2 = "@#//sanra12"; s1 = str1.split(''); s2 = str2.split(''); var i = s1.length + 1; while (i--) { if (s2.indexOf(s1[i]) >= 0) s2.splice(s2.indexOf(s1[i]), 1); } console.log(s2)

the resulting array represents the letters of str2 not matched in str1

this will return empty string if character set in both strings is the same.

function findDiff (str1, str2)
{
    var diff = '';

    if (str1.length > str2.length)
    {
        var search = str1;
        var compare = str2;
    }
    else
    {
        var search = str2;
        var compare = str1;
    }

    for (var i = 0; i < search.length; i++)
    {
        var symbol = search[i];

        if (compare.indexOf(symbol) === -1)
        {
            diff += symbol;
        }
    }
    return(diff);
}

findDiff("ansar@#//123", "@#//sanra");

https://jsfiddle.net/tadaspaplauskas/pn7jnj8e/

This will return a true or false , if character in both string having the same character, and i think this is the most efficent way to do that.

a)

function hasSameCharacter(str1, str2) {
    let a = Array.prototype.every.call(str1, (char) => str2.indexOf(char) > -1, this);
    if (a) return Array.prototype.every.call(str2, (char2) => str1.indexOf(char2) > -1, this);
    else return false;
}

console.log(hasSameCharacter(str1, str2));

b)

function hasSameCharacter(str1, str2) {
    for (let i = 0; i < str1.length; i++) {
        if (str2.indexOf(str1[i]) <= -1) return false;
    }
    for (let i = 0; i < str2.length; i++) {
        if (str1.indexOf(str2[i]) <= -1) return false;
    }
    return true;
}

console.log(hasSameCharacter(str1, str2));

Hope this helps, happy coding :)

This code may help you to get output as per your wish.

 var str1 = "ansar@#//g123"; var str2 = "@#//sanraD"; function strDiff(s1, s2){ var t,inter,a1,a2; a1 = s1.split(''); a2 = s2.split(''); t = a2, a2 = a1, a1 = t; inter = a1.filter(function (e) { if (a2.indexOf(e) !== -1) return true; }); for (i=0, len=inter.length; i<len; i++) { for(var j = a1.length-1; j>=0;j--) if (a1[j] === inter[i]) a1.splice(j, 1); for(var k = a2.length-1; k>=0; k--) if (a2[k] === inter[i]) a2.splice(k, 1); } if((a1.join('')+a2.join('')).length > 0) return(a1.join('')+a2.join('')); else return "True"; } var result = strDiff(str2,str1); alert(result);

So I can't comment, because I don't have the ratings, but I tried Yuriy Yakym's solution (the second one, and a massive thank you Yuriy because I'd have struggled without that to work with in the beginning) and whilst it works for this example, if you add another 'a' say to str1, it won't show you that str2 doesn't actually have 3 'a's, because it's always going to reference the first appearance of the said character. In essence, you need to delete the found characters to ensure you are accounting for duplicates, or so it seems.

I developed the below from Yuriy's code to get it working for me. Please test it yourself. Happy to be disagreed with, but it seems, it will log out everything (including duplicates) that appears in str1 but doesn't appear inside str2:

const sorter = (str1, str2) => {
   const arr = str1.split(""); 
   const newArray = arr.filter((c) => {
      if (str2.indexOf(c) === -1) {
         return str2.indexOf(c) === -1 
      } else {
         str2 = str2.replace(c, '');
      }   
   })
   return newArray.join("");
};

console.log(sorter(str1, str2));
function compare(str1, str2) {
  if (str1.length !== str2.length) return false
  let res = ''
  let str2Copy = str2
  for(let i = 0; i < str1.length; i++) {
    for(let j = 0; j < str2Copy.length; j++) {
        if(str1[i] === str2Copy[j]) {
        res += str2Copy[j]
        if (j === 0) {
            str2Copy = str2Copy.slice(1)
        } else if(j === str2Copy.length - 1) {
            str2Copy = str2Copy.slice(0, str2Copy.length - 1)
        } else {
            str2Copy = str2Copy.slice(0, j) + str2Copy.slice(j, str2Copy.length - 1)
        }
        break;
      }
    }
  }
  if(res.length !== str1.length) {
    return false
  } else {
    return true
  }
}

console.log(compare('tester', 'retset'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM