Sandy0109
BAN USERpackage careerCup;
import java.util.ArrayList;
import java.util.List;
/**
* <p>
* Giving a string and an string dictionary, find the longest string in
* dictionary which can formed by deleting some characters of the giving string.
* eg:S = abpcplea, Dict = {ale, apple, monkey, plea}, the return "apple"
* </p>
*
* @author
*
*/
//Added few conditions to check if there are multiple longest string with same length.
public class LongestStringAfterCharacterDelete {
List findLongestString(List<String> dict, String input) {
String finalString = null;
List<String> multipleLongestString = new ArrayList<String>();
int index = 0;
boolean isLongStringFound = false;
for (String tempDict : dict) {
finalString = compareString(tempDict, input);
if (tempDict.equals(finalString)) {
finalString = tempDict;
if (multipleLongestString.size() == 0) {
multipleLongestString.add(index, finalString);
} else if (finalString.length() > multipleLongestString.get(0)
.length()) {
isLongStringFound = true;
}
}
if (finalString.length() >= multipleLongestString.get(0).length()
&& !finalString.equals(multipleLongestString.get(0))) {
if (isLongStringFound) {
multipleLongestString.remove(0);
isLongStringFound = false;
}
multipleLongestString.add(0, finalString);
index++;
}
}
printList(multipleLongestString);
return multipleLongestString;
}
void printList(List<String> inputList) {
for (String longestString : inputList) {
System.out.println(longestString);
}
}
String compareString(String tempDict, String input) {
StringBuffer sb = new StringBuffer();
int tempCount = 0;
int inputStringLength = input.length();
int dictLength = 0;
while (inputStringLength != 0) {
if (dictLength < tempDict.length()) {
if (input.toLowerCase().contains(
Character.toString(tempDict.toLowerCase().charAt(
tempCount)))) {
sb.append(tempDict.charAt(tempCount));
tempCount++;
dictLength++;
}
}
inputStringLength--;
}
return sb.toString();
}
public static void main(String[] args) {
List<String> dict = new ArrayList<String>();
dict.add("ale");
dict.add("applee");
dict.add("monkey");
dict.add("plea");
String input = "abpcpleeaDChsssgtrsssmonkey";
LongestStringAfterCharacterDelete ls = new LongestStringAfterCharacterDelete();
ls.findLongestString(dict, input);
}
}
public class StringGroup {
// ----------------------------------------------------------------
public static String groupString(String inp, int grpLen) {
String grp = "";
int stringLen = inp.length();
StringBuffer inpString = new StringBuffer();
for (int i = 0; i < inp.length(); i = i + grpLen) {
if (stringLen >= grpLen) {
stringLen = stringLen - grpLen;
grp = inp.substring(i, i + grpLen);
inpString.append(grp + "-");
} else {
grp = inp.substring(i, inp.length());
inpString.append(grp);
}
}
return inpString.toString();
}
public static void main(String[] args) {
String inp = "California";
groupString(inp, 3);
}
}
public class ReverseToString {
public static void main(String[] args) {
String inp = "How Are You Doing Today";
boolean firstWord = false;
for (int i = inp.length() - 1; i >= 0; i--) {
if (firstWord == false) {
System.out.print(String.valueOf(inp.charAt(i)).toUpperCase());
firstWord = true;
} else if (String.valueOf(inp.charAt(i)).equals(" ")) {
System.out.print(" "
+ String.valueOf(inp.charAt(i - 1)).toUpperCase());
i = i - 1;
} else {
System.out.print(String.valueOf(inp.charAt(i)).toLowerCase());
}
}
}
}
public class StringExtract {
public static void extractString(String inp) {
String[] stringPattern = inp.split(" ");
for (int i = 0; i < stringPattern.length; i++) {
String temp = stringPattern[i];
if (!String.valueOf(temp.charAt(0)).equals("(")) {
if (temp.contains("(")) {
temp = temp.substring(0, temp.indexOf("("));
System.out.println(temp.replace(")", ""));
} else {
System.out.println(temp.replace(")", ""));
}
}
}
}
public static void main(String[] args) {
String inp = "(S (NP (NNP James)) (VP (VBZ is) (NP (NP (DT a)(NN boy))(VP (VBG eating) (NP (NNS sausages))))))";
//String inp = "(NP(DT a) (NN boy))";
extractString(inp);
}
}
1. Sort the array string by length
2. iterate over it and get longest and second longest string length
3. print it.
//-----Replace the string with any desired string and also key with any key.
import java.util.Arrays;
import java.util.Comparator;
public class LongestPhraseString
{
//~ Methods ----------------------------------------------------------------
public static void countString( String[] inp, int length )
{
Arrays.sort( inp, new Comparator<String>( )
{
public int compare( String s1, String s2 )
{
return s1.length( ) - s2.length( );
}
} );
int longestStringLen = 0;
String longestString = "";
int secondLongestStringLen = 0;
String secondLongestString = "";
int count = 0;
boolean flag = false;
for ( int i = inp.length - 1; i >= 0; i-- )
{
System.out.println( inp [i] );
if ( inp [i].length( ) <= length )
{
if ( count == 0 )
{
longestStringLen = inp [i].length( );
longestString = inp [i];
}
else if ( inp [i + 1].length( ) > inp [i].length( ) )
{
if ( flag == false )
{
secondLongestStringLen = inp [i].length( );
secondLongestString = inp [i];
flag = true;
}
}
count++;
}
}
System.out.println( "[" + secondLongestStringLen + " , " +
longestStringLen + "]" );
System.out.println( "[" + secondLongestString + " , " + longestString +
"]" );
}
public static void main( String[] args )
{
String inp = "I love chicken";
String[] inpStr = inp.split( " " );
countString( inpStr, 5 );
}
}
There are many approach some of them are like using data partition or mapreduce.
I have written code to handle small chunk of data to find top 5 most occurred words, this can be modified accordingly.
and
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
/**
* @author
*
*/
public class FrequentlyOccurredString {
static List<Entry<String, Integer>> listOfWords = new ArrayList<Entry<String, Integer>>();
public static void sortMap(HashMap<String, Integer> inputMap) {
Set<Entry<String, Integer>> set = inputMap.entrySet();
listOfWords = new ArrayList<Entry<String, Integer>>(set);
Collections.sort(listOfWords, new Comparator<Map.Entry<String, Integer>>() {
public int compare(Map.Entry<String, Integer> obj1,
Map.Entry<String, Integer> obj2) {
int value = (obj2.getValue()).compareTo(obj1.getValue());
return value;
}
});
}
public static void printMap(List<Entry<String, Integer>> list) {
int count = 0;
for (Map.Entry<String, Integer> wordOccurrence : list) {
if (count <= 4) {
System.out.println("'" + wordOccurrence.getKey() + "' - " + ""
+ wordOccurrence.getValue());
}
count++;
}
}
public static void findFrequentlyOccurredString(String inpString, String dictionaryString) {
String tempString = "";
int DEFAULT_VALUE = 1;
String[] storeStringToken = inpString.replace(" ", "").trim().split(" ");
HashMap<String, Integer> countStringOccurrence = new HashMap<String, Integer>();
for (int i = 0; i < storeStringToken.length; i++) {
// insert all new words to hashmap
if (!countStringOccurrence.containsKey(storeStringToken[i])) {
if (!dictionaryString.toLowerCase().contains(
storeStringToken[i].toLowerCase())) {
tempString = storeStringToken[i];
countStringOccurrence.put(tempString.trim(), DEFAULT_VALUE);
}
}
// else process existing words from hashmap
else {
tempString = storeStringToken[i];
for (Map.Entry<String, Integer> mapEntry : countStringOccurrence
.entrySet()) {
// check if the key already there
if (mapEntry.getKey().equals(tempString)) {
int keyOccurrence = mapEntry.getValue();
keyOccurrence++;
countStringOccurrence.put(tempString, keyOccurrence);
}
}
}
}
// sort hashmap in decending order
sortMap(countStringOccurrence);
// print hashMap
printMap(listOfWords);
}
public static void main(String[] args) {
String inp = "It is globally known for its Web portal, search engine Yahoo! Search, and related services, including Yahoo! Directory, Yahoo! Mail, Yahoo! News, Yahoo! Finance, Yahoo! Groups, Yahoo! Answers, advertising, online mapping, video sharing, fantasy sports, and its social media website.";
String dictionary = "andtheinorbetweenisaMrtheretheirthemthosetheybyonbutduringafterbeforeassuchreturnsfollowsfromtoaboveofoffwantsbecauseitsknown";
inp = inp.replaceAll("[^a-zA-Z\\s]", "");
findFrequentlyOccurredString(inp, dictionary);
}
}
and
Repmerrillagreerm, Android Engineer at Abs india pvt. ltd.
I am patient, compassionate, and caring when treating and counseling patients in practice, making them feel like they’re in ...
Repclarasbarr, Korean Air Change Flight at Adap.tv
I am ClaraBarr from California USA. Writes and records various different genres for television, film and other artists.Wrote several ...
- Sandy0109 February 03, 2017