Last active
August 7, 2019 10:53
-
-
Save vinaypuranik/0786a61d4c3beb1e3407388723fbc02f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
class Solution{ | |
public int solution(String S) { | |
List<String> sentences = new ArrayList<String>(); | |
List<Integer[]> dot = getLocations(S, "."); | |
List<Integer[]> question = getLocations(S, "?"); | |
List<Integer[]> exclaimation = getLocations(S, "!"); | |
int startIdx = 0; | |
startIdx = constructSentences(dot, S, startIdx, sentences); | |
startIdx = constructSentences(question, S, startIdx, sentences); | |
startIdx = constructSentences(exclaimation, S, startIdx, sentences); | |
if(startIdx < S.length()) { | |
sentences.add(S.substring(startIdx, S2.length())); | |
} | |
int maxCount = 0; | |
for(String s: sentences) { | |
String[] split = s.trim().split("\\s"); | |
maxCount = Math.max(maxCount, split.length); | |
} | |
return maxCount; | |
} | |
public List<Integer[]> getLocations(String str, String substring){ | |
List<Integer[]> locations = new ArrayList<Integer[]>(); | |
int startIdx = 0; | |
while(startIdx < str.length()) { | |
int nextIdx = str.indexOf(substring, startIdx); | |
if(nextIdx != -1 ) { | |
locations.add(new Integer[] {nextIdx, nextIdx+ substring.length()}); | |
startIdx = nextIdx +1; | |
} else { | |
break; | |
} | |
} | |
return locations; | |
} | |
public int constructSentences(List<Integer[]> locations, String string, int startIdx, List<String> sentences) { | |
for(Integer[] location: locations) { | |
sentences.add(string.substring(startIdx, location[0])); | |
startIdx = location[1]; | |
} | |
return startIdx; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment