比较两个字符串并计算相似字符的数量

时间:2020-05-24 19:14:21

标签: javascript

我正在尝试编写一个js函数,该函数比较两个字符串并返回相似事件的数量。但是我的功能是 也考虑重复。 例如,两个单词“ state”和“ tall”。理想情况下,它应返回2的计数,但考虑到“状态”中有两个重复的t,它将返回3。谁能帮我这个忙

4 个答案:

答案 0 :(得分:1)

这是ES5解决方案:

PLAY [local] *******************************************************************

TASK [Show VMs with disk space more than 60GB] *********************************
failed: [local] (item={'ansible_loop_var': 'item', 'changed': False, 'failed': False, 'guest_disk_info': {'0': {'backing_datastore': 'DatastoreA', 'backing_eagerlyscrub': False, 'backing_filename': '[DatastoreA] server1/server1.vmdk', 'backing_thinprovisioned': True, 'backing_type': 'FlatVer2', 'backing_uuid': '6100C29a-af30-ebe4-0c68-e72d60481103', 'backing_writethrough': False, 'capacity_in_bytes': 214748364800, 'capacity_in_kb': 209715200, 'controller_bus_number': 0, 'controller_key': 1000, 'controller_type': 'lsilogicsas', 'key': 2000, 'label': 'Hard disk 1', 'summary': '209,715,200 KB', 'unit_number': 0}}, 'invocation': {'module_args': {'datacenter': '/DatacenterA', 'folder': '/vm/server2', 'hostname': 'vmware.server.example.com', 'moid': None, 'name': 'server1', 'password': 'VALUE_SPECIFIED_IN_NO_LOG_PARAMETER', 'port': 443, 'proxy_host': None, 'proxy_port': None, 'use_instance_uuid': False, 'servername': 'server2', 'uuid': None, 'validate_certs': False}}, 'item': 'server1'}) => {
    "ansible_loop_var": "item",
    "assertion": "item.guest_disk_info['0'].capacity_in_kb <= 62914560",
    "changed": false,
    "evaluated_to": false,
    "item": {
        "ansible_loop_var": "item",
        "changed": false,
        "failed": false,
        "guest_disk_info": {
            "0": {
                "backing_datastore": "DatastoreA",
                "backing_eagerlyscrub": false,
                "backing_filename": "[DatastoreA] server1/server1.vmdk",
                "backing_thinprovisioned": true,
                "backing_type": "FlatVer2",
                "backing_uuid": "6100C29a-af30-ebe4-0c68-e72d60481103",
                "backing_writethrough": false,
                "capacity_in_bytes": 214748364800,
                "capacity_in_kb": 209715200,
                "controller_bus_number": 0,
                "controller_key": 1000,
                "controller_type": "lsilogicsas",
                "key": 2000,
                "label": "Hard disk 1",
                "summary": "209,715,200 KB",
                "unit_number": 0
            }
        },
        "invocation": {
            "module_args": {
                "datacenter": "/DatacenterA",
                "folder": "/vm/server2",
                "hostname": "vmware.server.example.com",
                "moid": null,
                "name": "server1",
                "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
                "port": 443,
                "proxy_host": null,
                "proxy_port": null,
                "servername": "server2",
                "use_instance_uuid": false,
                "uuid": null,
                "validate_certs": false
            }
        },
        "item": "server1"
    },
    "msg": "server1 disk space is too high, it must be below 60GB"
}
ok: [local] => (item={'ansible_loop_var': 'item', 'changed': False, 'failed': False, 'guest_disk_info': {'0': {'backing_datastore': 'DatastoreA', 'backing_eagerlyscrub': False, 'backing_filename': '[DatastoreA] server2/server2.vmdk', 'backing_thinprovisioned': False, 'backing_type': 'FlatVer2', 'backing_uuid': '6000D296-4d2a-abf0-5d21-0cfcdf2498ad', 'backing_writethrough': False, 'capacity_in_bytes': 37580963840, 'capacity_in_kb': 36700160, 'controller_bus_number': 0, 'controller_key': 1000, 'controller_type': 'lsilogicsas', 'key': 2000, 'label': 'Hard disk 1', 'summary': '36,700,160 KB', 'unit_number': 0}}, 'invocation': {'module_args': {'datacenter': '/DatacenterA', 'folder': '/vm/server2', 'hostname': 'vmware.server.example.com', 'moid': None, 'name': 'server2', 'password': 'VALUE_SPECIFIED_IN_NO_LOG_PARAMETER', 'port': 443, 'proxy_host': None, 'proxy_port': None, 'use_instance_uuid': False, 'servername': 'server2', 'uuid': None, 'validate_certs': False}}, 'item': 'server2'}) => {
    "ansible_loop_var": "item",
    "changed": false,
    "item": {
        "ansible_loop_var": "item",
        "changed": false,
        "failed": false,
        "guest_disk_info": {
            "0": {
                "backing_datastore": "DatastoreA",
                "backing_eagerlyscrub": false,
                "backing_filename": "[DatastoreA] server2/server2.vmdk",
                "backing_thinprovisioned": false,
                "backing_type": "FlatVer2",
                "backing_uuid": "6000D296-4d2a-abf0-5d21-0cfcdf2498ad",
                "backing_writethrough": false,
                "capacity_in_bytes": 37580963840,
                "capacity_in_kb": 36700160,
                "controller_bus_number": 0,
                "controller_key": 1000,
                "controller_type": "lsilogicsas",
                "key": 2000,
                "label": "Hard disk 1",
                "summary": "36,700,160 KB",
                "unit_number": 0
            }
        },
        "invocation": {
            "module_args": {
                "datacenter": "/DatacenterA",
                "folder": "/vm/server2",
                "hostname": "vmware.server.example.com",
                "moid": null,
                "name": "server2",
                "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
                "port": 443,
                "proxy_host": null,
                "proxy_port": null,
                "servername": "server2",
                "use_instance_uuid": false,
                "uuid": null,
                "validate_certs": false
            }
        },
        "item": "server2"
    },
    "msg": "All assertions passed"
}

PLAY RECAP *********************************************************************
local                      : ok=0    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   

答案 1 :(得分:0)

下次您必须先发布代码,但这对您来说应该有用:

const countSimilarities = (s1, s2) =>
  s1
    .split("")
    .map((char1) => s2.split("").find((char2) => char1 === char2))
    .reduce((acc, c) => (c ? acc + 1 : acc), 0);

答案 2 :(得分:0)

可能可以对此进行优化,但这是进行此检查的函数的相当简单的版本。

function matchingLettersCount(s1, s2) {
  const letters1 = [...new Set(s1.split(''))];
  const letters2 = [...new Set(s2.split(''))];
  return letters1.filter(x => letters2.includes(x)).length;
}

console.log(matchingLettersCount('state', 'tall'));

答案 3 :(得分:0)

这可以帮助吗?我希望:如果希望区分大小写,请将“ gi”替换为“ g”。

class CustomTooltip extends Component {
  tooltipId = `Tooltip-${this.props.name}-${Math.random().toString(36).substr(2, 5)}`

...