Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

javascript 解决回文索引问题 #15

Open
sunhengzhe opened this issue Apr 19, 2018 · 5 comments
Open

javascript 解决回文索引问题 #15

sunhengzhe opened this issue Apr 19, 2018 · 5 comments

Comments

@sunhengzhe
Copy link
Member

sunhengzhe commented Apr 19, 2018

给定一个单词 word 和一个字符串 S,找到 S 中的所有 word 及其回文(逆序)的起始索引

function findAnswer(word, s) {
	// TODO
}

findAnswer('abc', 'abcxabcbacc'); // [0, 4, 6]
@liliuzhu
Copy link

比较笨的方法,也可以实现

findAnswer(word, str) {
    if (typeof str !== 'string' || typeof word !== 'string') {
        throw new Error('参数类型错误');
    }
    let reverse = word.split('').reverse().join('');
    let reg = new RegExp(word + '|' + reverse, 'g');
    let result = null;
    let indexArr = [];
    while ((result = reg.exec(str)) != null) {
        indexArr.push(result.index);
        reg.lastIndex = result.index + 1;
    }
    console.log(indexArr);
}

@andysongs
Copy link

const check = (str1, str2) => {
  return (str1 === str2) || (str1 === str2.split('').reverse().join(''))
}

const findAnswer = (word, s) => {
  // TODO
  if (word.length > s.length) return
  let length = word.length
  let i = 0;
  let res = [];
  while (s[i + length - 1]) {
    let sample = s.slice(i, i + length)
    console.log(sample)
    if (check(word, sample)) res.push(i)
    i++;
  }
  return res
}

console.log(findAnswer('abc', 'abcxabcbacc'));

@wycnewu
Copy link

wycnewu commented Apr 19, 2018

if (check(word, sample)){ res.push(i); i = i + length} else{ i++ }

@jiangyanyun
Copy link

function findAnswer(word, s) {
let indexs = [];
let drow = word.split('').reverse().join('');
let regExp = new RegExp(${word.replace(/(\w)$/,'(?=$1)')}|${drow}, 'g');
s.replace(regExp, function(z, b) {
indexs.push(b)
});
return indexs;
}

@iLove-Coding
Copy link

        function findAnswer(word, s) {
            return [...getStr(word, s), ...getStr(reverseString(word), s)];
        }

        function getStr(word, s) {
            let s_copy = s;
            let arr = [];
            let temp = 0;
            while (true) {
                let index = s_copy.search(word);
                if (index !== -1) {
                    s_copy = s_copy.slice(index + word.length);
                    arr.push(temp + index);
                    temp = temp + index + word.length; //指针,开始在字符串首位置
                } else {
                    break;
                }
            }
            return arr;
        }

        function reverseString(str) {
            var newstr = str.split("").reverse().join("");
            return newstr;
        }

       findAnswer('abc', 'abcxabcbacc'); // [0, 4, 6]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants