We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
staticlongsubstrCount(intn,strings){longspecialCnt=0;// step 1: count special subs only with the consecutive same charsfor(inti=0;i<n;i++){intlen=0;while(i+len<n){if(s[i+len]==s[i]){specialCnt++;}else{break;}len++;}}// step 2: count special subs with one different char in the middlefor(inti=1;i<n;i++){charc=s[i-1];if(c!=s[i]){intoffset=1;while(i-offset>=0&&i+offset<n&&s[i-offset]==c&&s[i+offset]==c){specialCnt++;offset++;}}}returnspecialCnt;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Special String Again
You are viewing a single comment's thread. Return to all comments →
Accepted C# code, easy to understand. O(n^2).