Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Result {
- /*
- * Complete the 'weightedUniformStrings' function below.
- *
- * The function is expected to return a STRING_ARRAY.
- * The function accepts following parameters:
- * 1. STRING s
- * 2. INTEGER_ARRAY queries
- */
- public static List<String> weightedUniformStrings(String s, List<Integer> queries) {
- // Write your code here
- List<String> result = new ArrayList<>();
- //List<Integer> weights = new ArrayList<>();
- //List<Integer> charValues = s.chars().mapToObj(c -> (int)(char)c-96).collect(Collectors.toList());
- HashSet<Integer> weights = new HashSet<>();
- char[] letters = s.toCharArray();
- int amount = 1;
- int weight;
- for (int i = 0; i < letters.length; i++){
- weight = letters[i]-96;
- weights.add(weight*amount);
- try{
- if (Character.compare(letters[i], letters[i+1]) == 0){
- amount++;
- }
- else {
- amount = 1;
- }
- } catch (ArrayIndexOutOfBoundsException e){}
- }
- queries.forEach(i -> {
- if (weights.contains(i)){
- result.add("Yes");
- } else {
- result.add("No");
- }
- });
- /*for (Integer i : queries){
- if (weights.contains(i)){
- result.add("Yes");
- } else {
- result.add("No");
- }
- }*/
- /*List<Integer> weights = new ArrayList<>();
- char[] letters = s.toCharArray();
- for (int i = 0; i <letters.length; i++){
- int weight = (int)letters[i]-96;
- int amount = 1;
- for (int j = i+1 ; j < letters.length; j++){
- weights.add(weight*amount);
- if (letters[j] != letters[i]){
- i = j-1;
- break;
- }
- amount++;
- }
- weights.add(weight*amount);
- }
- for (Integer i : queries){
- if (weights.contains(i)){
- result.add("Yes");
- } else {
- result.add("No");
- }
- }*/
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement