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.
I tried to find a way to implement a fix that somehow relates to the "Sparse Array" concept, but in sparse array we convert the array into a linked a list with the non empty entries only to save storage, so I imagined that converting the sparse array of word characters to a linked list is the way to go, here is my try:
usingSystem;usingSystem.Collections.Generic;usingSystem.IO;classSolution{staticvoidMain(String[]args){/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */intstrCount=int.Parse(Console.In.ReadLine());for(inti=0;i<strCount;i++){stringstr=Console.In.ReadLine();AddWord(str);}intqueryCount=int.Parse(Console.In.ReadLine());for(inti=0;i<queryCount;i++){stringstr=Console.In.ReadLine();actual_WordCount(str);}}staticWordNodeStartWordNode=null;staticWordNodeLastWordNode=null;publicstaticvoidAddWord(stringword){//Every new word add new word node if(StartWordNode==null)LastWordNode=StartWordNode=newWordNode();elseLastWordNode=LastWordNode.NextWordNode=newWordNode();CharacterNodecurrentCharacter=null;for(inti=0;i<word.Length;i++){if(currentCharacter==null)currentCharacter=LastWordNode.Character=newCharacterNode();elsecurrentCharacter=currentCharacter.NextCharacterNode=newCharacterNode();currentCharacter.Char=word[i];currentCharacter.Position=i;}}privatestaticvoidactual_WordCount(stringword){intcount=0;WordNodecurrentWord=StartWordNode;while(currentWord!=null){CharacterNodecurrentCharNode=currentWord.Character;boolallCharsFound=true;for(inti=0;i<word.Length;i++){if(currentCharNode.Char!=word[i]||currentCharNode.Position!=i||(i==word.Length-1&¤tCharNode.NextCharacterNode!=null)){allCharsFound=false;break;}currentCharNode=currentCharNode.NextCharacterNode;}if(allCharsFound)count++;currentWord=currentWord.NextWordNode;}Console.WriteLine(count);}publicstaticvoidcountWords(){intstrCount=int.Parse(Console.In.ReadLine());for(inti=0;i<strCount;i++){stringstr=Console.In.ReadLine();AddWord(str);}intqueryCount=int.Parse(Console.In.ReadLine());for(inti=0;i<queryCount;i++){stringstr=Console.In.ReadLine();actual_WordCount(str);}}}publicclassCharacterNode{publiccharChar{get;set;}publicintPosition{get;set;}publicCharacterNodeNextCharacterNode{get;set;}}publicclassWordNode{publicintWordCount{get;set;}publicCharacterNodeCharacter{get;set;}publicWordNodeNextWordNode{get;set;}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Sparse Arrays
You are viewing a single comment's thread. Return to all comments →
I tried to find a way to implement a fix that somehow relates to the "Sparse Array" concept, but in sparse array we convert the array into a linked a list with the non empty entries only to save storage, so I imagined that converting the sparse array of word characters to a linked list is the way to go, here is my try: