Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const checkIfMirror = string => {
- const arr = string.split('').sort();
- let removed = []
- let index = 1;
- console.log(`${arr} <=========== sorted`)
- while (index <= arr.length) {
- if (arr[index] !== arr[index - 1] && arr[index] !== arr[index + 1]) {
- removed.push(arr.splice(arr.indexOf(arr[index]), 1));
- } else if (arr[arr.length - 1] !== arr[arr.length - 2]) {
- removed.push(arr.splice(arr.indexOf(arr[arr.length - 1])))
- } else if (arr[0] !== arr[1]) {
- removed.push(arr.splice(0, 1))
- }
- index++
- }
- index = 0;
- while (index <= arr.length) {
- if (arr[index] !== arr[index - 1] && arr[index] !== arr[index + 1]) {
- removed.push(arr.splice(arr.indexOf(arr[index]), 1));
- } else if (arr[arr.length - 1] !== arr[arr.length - 2]) {
- removed.push(arr.splice(arr.indexOf(arr[arr.length - 1])))
- } else if (arr[0] !== arr[1]) {
- removed.push(arr.splice(0, 1))
- }
- index++
- }
- console.log(`${arr} <=========== cleaned`)
- console.log(`${removed.join()}<========= removed`)
- let curSequence = 0;
- let longestSeq = 0;
- for (let i = 0; i < arr.length; i++) {
- if (arr[i] === arr[i + 1]) {
- curSequence++
- } else {
- if (curSequence > longestSeq) {
- longestSeq = curSequence;
- curSequence = 0;
- } else {
- curSequence = 0;
- }
- }
- }
- console.log(`${longestSeq} <========== longest seq`)
- if (arr.length % 2 === 0 && removed.length <= 1) {
- return true;
- } else if (arr.length % 2 !== 0 && removed.length === 0) {
- return true;
- } else {
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment