- 写一个函数判断字符串中x的数量和o的数量是否相等(忽略大小写)
XO("ooxx") => true
XO("xooxx") => false
XO("ooxXm") => true
XO("zpzpzpp") => true // 没有x也没有o,所有相等,都为0
XO("zzoo") => false
- 几种解法分析:
function XO(str) {
if (typeof str !== 'string') return 'not a string'
return str.replace(/x/ig, '').length === str.replace(/o/ig, '').length
}
function XO(str) {
return (str.split('').filter(v => v === ('o'||'O')).length) === (str.split('').filter(v => v === ('x'||'X')).length)
}
function XO(str) {
return (str.split('').filter(v => v === ('o'||'O')).length) === (str.split('').filter(v => v === ('x'||'X')).length)
}
function XO(str) {
let len = 0;
[...str.toUpperCase()].forEach((char) => {
if('X' === char) {
len++;
} else if ('O' === char) {
len--;
}
});
return len === 0;
}
let XO = (str) => {
let obj = str.toLowerCase().split('').reduce((pre,cur)=>{
pre[cur]?pre[cur]++:pre[cur]=1;
return pre;
},{})
return obj['o']==obj['x'];
}
let XO=(str)=>{
return [...str].filter(item=>item.toLowerCase()==='o').length===[...str].filter(item=>item.toLowerCase()==='x').length
};
function XO(str){
return (str.match(/x/ig) || []).length === (str.match(/o/ig) || []).length;
}
const XO = str => {
if (!str) {
return true;
}
let oLen = 0;
let xLen = 0;
str.toLowerCase().replace(/./g, item => {
if (item === 'o') {
oLen += 1;
}
if (item === 'x') {
xLen += 1;
}
});
return oLen === xLen;
};
const XO1 = str => {
if (!str) {
return true;
}
const strAttr = str.toLowerCase().split('');
const oLen = strAttr.filter(item => item === 'o').length;
const xLen = strAttr.filter(item => item === 'x').length;
return oLen === xLen
};
let XO = str => {
let o = str.match(/o/ig) || [];
let x = str.match(/x/ig) || [];
return o.length === x.length;
}
const XO = str => {
let numberO = 0, numberX = 0
str.toLowerCase().split('').forEach(v => {
if (v === 'o') {
numberO += 1
} else if (v === 'x') {
numberX += 1
}
})
return numberO === numberX
}
const XO = str => {
const newStr = str.toLowerCase().split('').sort().join('')
const numberO = /o+/.exec(newStr) ? /o+/.exec(newStr)[0].length : 0
const numberX = /x+/.exec(newStr) ? /x+/.exec(newStr)[0].length : 0
return numberO === numberX
}
const XO = (str, config = {left: 'o', right: 'x'}) => {
str = str.toLocaleLowerCase();
const leftArr = [];
const rightArr = [];
[...str].forEach((key) => {
key === config.left && leftArr.push(key);
key === config.right && rightArr.push(key);
})
return leftArr.length === rightArr.length;
};
function xo (str) {
// 判断是否含有xo
str = str.toLowerCase()
if (!str.includes('x') || !str.includes('o')) return false
// 遍历获取个数
let xlen = 0
let olen = 0
str.split('').forEach(element => {
if (element == 'x') {
xlen++
} else {
olen++
}
})
if (xlen == olen) return true
return false
}
function XO(str){
let ary =str.toLowerCase().match(/(o|x)/g);
return !ary||ary.filter(x=>x=='x').length==ary.filter(x=>x=='o').length?true:false;
}
const XO = function (str) {
let arr = str.split("");
let xCount = 0,
oCount = 0;
arr.forEach(item => {
if (item.toLowerCase() === "x") {
xCount += 1;
}
if (item.toLowerCase() === "o") {
oCount += 1;
}
});
return (xCount === oCount);
}
const XO = (str) => {
let strArr = str.toLowerCase().split('');
let o = strArr.filter(one=>one==='o').length;
let x = strArr.filter(one=>one==='x').length;
return o == x
}
function XO(val){
return val.split("").filter((item)=>(item.toLowerCase()=='x')).length===val.split("").filter((item)=>(item.toLowerCase()=='o')).length
}
function XO(str) {
let xoMap = str.split('').reduce((res, cur) => {
cur = cur.toLowerCase()
if (!res[cur]) {
res[cur] = 1
} else {
res[cur]++
}
return res
}, {})
return xoMap['o'] === xoMap['x']
}
let XO = (str)=>{
let o = str.split('').reduce((pre,current,index)=>{
current = current.toLocaleLowerCase()
current in pre ? pre[current]++ : pre[current]=1
return pre
},{x:0,o:0})
return o.x === o.o
}
function XO(str) {
return str.toLowerCase().split('o').length === str.toLowerCase().split('x').length
}
//思路一:转成数组并遍历,记录x和o的数量,对比是否相等
function XO(s) {
let count = {
x: 0,
o: 0
};
let a = s.toLowerCase().split("").forEach(item=> {
item == 'x' ? count.x++ : (item == 'o' ? count.o++: '');
});
return count.x == count.o
}
//思路二:基数为0,遇到x自增1,遇到o自减1 都不是则不增不减。 遍历后为0代表相等
function XO2(s) {
s = s.toLowerCase();
let count = 0;
for(var i = 0; i < s.length; i++) {
if(s.charAt(i) == 'x') {
count++
}
if(s.charAt(i) == 'o'){
count--;
}
}
return count == 0;
}
function xo(str=String) {
let x = str.match(/x/gi) !== null ? str.match(/x/gi).length : 0
let o = str.match(/o/gi) !== null ? str.match(/o/gi).length : 0
return x === o ? true :false
}
let aa = test('zzoo')
console.log(aa);
function test(str) {
return str.match(/o|O/g) && str.match(/x|X/g) ? str.match(/o|O/g).length === str.match(/x|X/g).length : str.match(/o|O/g) === str.match(/x|X/g)
}
function xo(str){
let {x,o} = [...str].reduce((obj,item)=> item.toLowerCase() == 'x' ? { ...obj,x: obj.x + 1 } : (item.toLowerCase() == 'o' ? { ...obj,o: obj.o + 1 }:obj),{x:0,o:0})
console.log(x == o)
}
const XO = (str) => {
const arr = str.split('')
let obj = {}
arr.forEach(d => {
let s = d.toLocaleLowerCase()
obj[s] = obj[s] ? obj[s] += 1 : 1
})
return obj.o === obj.x
}
function XO(str) {
if(str.indexOf('x')>=0 && str.indexOf('o')>=0){
if(str.match(/x/ig).length == str.match(/o/ig).length){
return true;
}
return false;
}
else{
console.log('字符串不包含x或o')
}
}
function XO(str) {
const arr = str.split('');
return strLength(arr, 'o') === strLength(arr, 'x')
function strLength(arr, letter) {
return arr.filter(i => i.toLowerCase() === letter).length
}
}
function XO(str) {
let s = str.toLowerCase();
if (s.indexOf("o") < 0 || s.indexOf("x") < 0) {
return false
}else{
var rex = new RegExp("x", "g");
let reo = new RegExp("o", "g");
return Object.is(s.match(rex).length, s.match(reo).length)
}
}
/**
* @desc 写一个函数判断字符串中x的数量和o的数量是否相等(忽略大小写)
* @params {string}
* @return {Boolean} true/false
**/
const XO = (str) => {
let array = str.toLowerCase().split('');
let result = array.reduce((pre,cur) => {
pre[cur] ? pre[cur]++ : pre[cur] = 1;
return pre;
},{})
return result['o'] == result['x'];
}
function XO(arr) {
let x = o = 0;
for (let i = 0; i < arr.length; i++) {
if (arr[i] === 'x' || arr[i] === 'X') {
x += 1;
continue;
}
if (arr[i] === 'o' || arr[i] === 'O') {
o += 1;
}
};
return x === o;
}
function XO(str){
let x=str.match(/x/ig);//求出x的数组
let o=str.match(/o/ig);//求出o的数组
if(x&&o){
return x.length==o.length?true:false;
}else if(!x&&!o){
return true
}else {
return false;
}
}
const XO = str => {
let result = false
if(str.match(/o/ig) && str.match(/x/ig))
result = str.match(/o/ig).length == str.match(/x/ig).length ? true: false
return result
}
function XO(str){
let obj = str.split('').reduce((a,b)=>{
b = b.toLowerCase()
a[b]?++a[b]:a[b]=1
return a
},{})
if(obj.x==obj.o){return true}else {
return false
}
}
let XO=(str)=>{
return [...str].filter(item=>item.toLowerCase()==='o').length===[...str].filter(item=>item.toLowerCase()==='x').length
}
function XO(str){
let xresult = str.match(/x/ig) || []
let oresult = str.match(/o/ig) || []
return xresult.length === oresult.length
}
思路 1:转换成小写,正则匹配成数组,比较匹配不同正则表达式的两个数组的 length
let XO = str => { let getArr = reg => str.match(reg) || []; return getArr(/x/ig).length === getArr(/o/ig).length; }
思路 2:转换小写替换掉非 x 和 o 的元素,在回调中判断为 x,num 递增,为 o,num 递减,num 为 0 相等,否则不等
let XO = str => { let num = 0; str.toLowerCase().replace(/[(x|o)]/g, (…args) => args[0] === ‘x’ ? num++ : num—); return !num; }
思路 3:转换成小写替换掉非 x 和 o 的元素为空并转换成数组,过滤数组对立项,拿返回的数组长度比较
let XO = str => { let result = str.toLowerCase().replace(/[^(x|o)]/g, ‘’).split(‘’); let getLen = word => result.filter(val => val === word).length; return getLen(“x”) === getLen(“o”); }
思路 4:先将字符出转换小写并转换为数组,使用 reduce 归并设置作为缓存的初始值,此时缓存只存在 x、o 和其他键,归并过程中存在归并项加 1,不存在设置为 1,最后比较缓存中 x 和 o 为键的值是否相等
let XO = str => { let cache = str.toLowerCase().split(‘’).reduce((cache, val) => { cache[val] ? cache[val]++ : cache[val] = 1; return cache; }, {}); return cache[‘x’] === cache[‘o’]; }
思路 5:转换小写并转换数组,过滤并保证数组中只存在 x 和 o,while 循环检查数组中是否含有其中一项,维护循环次数,对比数组长度减循环次数的差值与循环次数是否相等
let XO = str => { let num = -1; let count = 0; let pureArr = str.toLowerCase().split(‘’).filter(val => val === “x” || val ===”o”); while ((num = pureArr.indexOf(“x”, num + 1)) !== -1) { count++; } return count === (pureArr.length - count); }
测试
console.log(XO(“ooxx”)); // true console.log(XO(“xooxx”)); // false console.log(XO(“ooxXm”)); // true console.log(XO(“zpzpzpp”)); // true console.log(XO(“zzoo”)); // false
```
```javascript
function XO(arg){
let o = arg.toLowerCase().match(/o/g);
let x = arg.toLowerCase().match(/x/g);
return (o && o.length) === (x && x.length)
}
function ox(str) {
let strAry=str.split('');
let newO=strAry.map(item=>item.toLowerCase()=='o');
let newX=strAry.map(item=>item.toLowerCase()=='x');
return newO.length===newX.length?true:false;
}
const XO = str => {
let st = str.toLowerCase();
let countX = 0;
let countO = 0;
for (let i of st) {
if (i === 'o') {
countO++;
} else if (i === 'x') {
countX++;
}
}
return countX == countO;
};
function XO(str){
let xcount=0,ocount=0;
for ( let i=0;i<str.length;i++){
if(str[i]=='o'){
ocount++
}
if(str[i]=='x'){
xcount++
}
}
if(xcount===ocount){
return true;
}else{
return false;
}
}
let XO = (str) => {
return [...str].filter(item => (item.toLowerCase() == 'o')).length == [...str].filter(item => (item.toLowerCase() == 'x')).length
}
let XO = (str) => {
let obj = {o:0,x:0};
[...str].forEach(item => {obj[item.toLowerCase()]++;})
return obj.o == obj.x;
}
function XO(str) {
var arr = str.split('').reduce((arr, next) => {
var char = next.toLowerCase();
if (char === 'o') arr[0] ? arr[0] ++ : arr[0] = 1
else if (char === 'x') arr[1] ? arr[1]++ : arr[1] = 1;
return arr;
}, [])
return arr[0] === arr[1]
}
function XO(str) {
return str.match(/o/ig).length === str.match(/x/ig).length
}
function XO(str){
let count = 0
str.split('').map(v=>{
v.toLowerCase() === 'x' && count++
v.toLowerCase() === 'o' && count--
})
return !count
}
function XO (str) {
let x = str.match(/[x]/gi) || []
let o = str.match(/[o]/gi) || []
console.log(x.length === o.length)
}
const XO = (str) => {
let arr = str.split('');
return arr.filter(el => el.toLowerCase() === 'o').length === arr.filter(el => el.toLowerCase() === 'x').length
};
function XO (str) {
let o = [...str].filter(item => {
return item.toLocaleLowerCase() === 'o'
})
let x = [...str].filter(item => {
return item.toLocaleLowerCase() === 'x'
})
return o.length === x.length
}
function XO(str) {
let wObj = [...str.toLowerCase()].reduce((obj, v) =>(obj[v] = obj[v] ? obj[v] + 1 : 1)&&obj,{})
return wObj['o']===wObj['x'];
}
function XO(str) {
return [...str.toLowerCase()].filter(val=>val==='x').length===[...str.toLowerCase()].filter(val=>val==='o').length
}
function XO(str) {
if (typeof str !== 'string') return;
let obj = str.split('').reduce((a, b) => {
b = b.toLowerCase()
a[b] ? a[b] += 1 : a[b] = 1
return a
}, {});
return (obj.o || 0) === (obj.x || 0)
}
function XO(str){
if (typeof str !== 'string') return;
let X = str.match(/x/gi) || [];
let O = str.match(/o/gi) || [];
return X.length === O.length
}
function XO(str) {
if (typeof str !== 'string') return;
let res = str.toLowerCase().split('').reduce((a, b) => {
return a + (b === 'x'? 1 : (b === 'o') ? -1 : 0)
}, 0);
return res == 0
}