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.
Here's the approch that I implement to solve this problem with Kotlin:
funsherlockAndAnagrams(s:String):Int{varwindowedSize=s.length-1// define possible windowed steps for that stringvarlistOfWindows:List<List<Char>>=listOf()varanagramsCounter=0// itterate based on possible windowed stepswhile(windowedSize>0){// get all windowed setslistOfWindows=s.toList().windowed(size=windowedSize,step=1){it->it.sortedBy{it}}// count all anagramslistOfWindows.forEachIndexed{index,window->for(jin(index+1)..listOfWindows.lastIndex){if(window==listOfWindows[j])anagramsCounter++}}windowedSize--// reduce the windowed size}returnanagramsCounter}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Sherlock and Anagrams
You are viewing a single comment's thread. Return to all comments →
Here's the approch that I implement to solve this problem with Kotlin: