记录

164 阅读1分钟

记录

  1. 输入: "you got a message"
    输出: "You Got A Message A Got You"
let input =  "you got a message"
    let arr = input.split(" ");
    let arr1 = [];
    let arr2 = [];
    for(let i=0;i<arr.length;i++){   
   let a =  arr[i].charAt(0).toUpperCase();
   let b = arr[i].substring(1,arr[i].length)
    arr[i] = a + b
    console.log(arr[i])
    arr1.push(arr[i]);
    if(i != arr.length-1){
        arr2.unshift(arr[i])
    } 
    }
    console.log(arr1.concat(arr2)); // "You Got A Message A Got You"
  1. 输入:"abbcccdeff"
    输出:"ccc" (最长重复子串)
let input = 'abbcccdeff';
let left = 0;
let right = 1;
let arr =[];
while(left<right && right<input.length ){
    if(input.charAt(left) != input.charAt(right)){
        left++;
        right++;
        continue;
    } else{
        right++
    }
    arr.push(input.substring(left,right)) 
}
console.log(arr[arr.length-1]); //ccc
  1. 输入: ["flower", "flow", "flight"]
    输出:fl
// 字符串共同子串前缀
      let strs = ["flower", "flow", "flight"];
      let lens = strs.map((item) => item.length);
      let minLen = Math.min(...lens);
      // let minLen = Math.min.apply(null, lens);

      let str = strs[lens.indexOf(minLen)];
      let right = minLen;
      while (right > 0) {
        let str1 = str.substring(0, right);
        if (strs.every((item) => item.includes(str1))) {
          console.log(str1);
          break;
        } else {
          right--;
        }
      }