1-
function DNAstr(s){ //看出bug请指出 if(s.length>=1&&s.length<=50){ var rege=/^[A-Z]+$/g; if(rege.exec(s)){ var reg=/[ATCG]{1,}/g; var tmp= s.match(reg); var lens=[]; var max=0; for(var key in tmp){ lens.push( tmp[key].length); } for(var key in lens){ if(lens[key] > max){ max = lens[key]; } } return max; } } } var s="TARCAT"; console.log(DNAstr(s));
function isParam(ch){ return ch == 'A' || ch == 'T' || ch == 'C' || ch == 'G'; } function DNAStr(str){ var i = 0; var maxlen = 0; var maxSr = ""; while(i < sttr.length){ var len = 0; if(isParam(str[i])){ while(i<str.length && isParam(str[i])) {i++;len++;} }else { i++; } if(maxlen < len){ maxlen = len; maxStr = str.slice(i - len, i); } } console.log(maxStr); return maxlen; } console.log(DNAStr("TARACTGACTGHT"));
2-
function isSame(str){ //判断是否偶串 var begin = 0 var end = str.length /2 while(end < str.length && str[begin] == str[end] ){ begin++ end++ } if( end == str.length ){ return true } return false } function isOString(str){ //从末尾删除若干个字符后,判断是否还是偶串 var max = 0 if(str.length <= 1){ return str.length } while(str.length >=2 ){ str = str.slice(0,str.length- 2) if(isSame(str)){ //调用偶串判断方法 return str.length } } return 0 } console.log(isOString("abaababaab"))
3-
function reverseStr(str){ //回文串 var mp={}; for(var key in str){ if(!mp[str[key]]){ mp[str[key]]=1; }else{ mp[str[key]]++; } } var odd=0;//mp中字符个数为奇数的个数 for(var key in mp){ if(mp[key]%2!=0){ odd++; } } return odd ? odd : 1; } console.log(reverseStr("abbaa"));//1 console.log(reverseStr("abc"));//3 console.log(reverseStr("abcabc"));//1
ps:看出bug的帮忙指出,谢