java - Storing hashmap values as program iterates -
i trying solve programming challenge outlined below, wherein need find word in given sentence greatest number of repeated characters. have struggled bit this, , lucky enough find code counting occurrences of letters in string (also below). particular piece of code store letters in hashmap, , need tailor stores character occurrence of each word separately (instead of in aggregate, doing @ moment). stuck. use store state of hashmap each iteration of loop?
/* using java language, have function lettercounti(str) take * str parameter being passed , return first word * greatest number of repeated letters. example: "today, * greatest day ever!" should return greatest because has 2 e's * (and 2 t's) , comes before ever has 2 e's. if there * no words repeating letters return -1. words * separated spaces. */ import java.util.arraylist; import java.util.hashmap; import java.util.map; public class othercountletters { void countletters2(string str) { string[] words = str.tolowercase().split(" "); map<character, integer> numchars = new hashmap<character, integer>(); (int = 0; < words.length; i++) { (int j = 0; j < words[i].length(); j++) { char charat = words[i].charat(j); if (!numchars.containskey(charat)) { numchars.put(charat, 1); } else { numchars.put(charat, numchars.get(charat) + 1); } } } system.out.println(numchars); } public static void main(string[] args) { othercountletters ocl = new othercountletters(); ocl.countletters2("today greatest day ever"); } }
at moment, sentence "today greatest day ever", program returns
{v=1, g=1, d=2, e=5, t=4, s=2, r=2, a=3, o=1, h=1, y=2, i=1}
but need return like
{a=1, d=1, o=1, t=1, y=1} //'today' {i=1, s=1} //'is' {e=1, h=1, t=1} //'the' {g=1, t=2, e=2, s=1, r=1, a=1} //'greatest' {d=1, a=1, y=1} //'day' {v=1, e=2, r=1} //'ever'
that way, iterate on each entry see 1 has largest value, , return corresponding word user.
thanks,
-----edit----
after posting had eureka moment:
import java.util.arraylist; import java.util.hashmap; import java.util.map; public class othercountletters { void countletters2(string str) { string[] words = str.tolowercase().split(" "); string target = null; int largest = 0; map<character, integer> numchars = new hashmap<character, integer>(); (int = 0; < words.length; i++) { (int j = 0; j < words[i].length(); j++) { char charat = words[i].charat(j); if (!numchars.containskey(charat)) { numchars.put(charat, 1); } else { numchars.put(charat, numchars.get(charat) + 1); } if (numchars.get(charat) > largest) { largest = numchars.get(charat); target = words[i]; } } numchars.clear(); } if (largest != 1) { system.out.println(target); } else { system.out.println("there no words 2 or more letters"); } } public static void main(string[] args) { othercountletters ocl = new othercountletters(); ocl.countletters2("today greatest day ever , car"); } }
have consider split phrase word , iterate on every word using method 'othercountletters'?
instead of returning nothing, return highest score of repeated character. then, in loop , have compare current maximum
at end of loop, might able give word of phrase highest repeated character
Comments
Post a Comment