Advertisement
YChalk

WeightedUniformStrings

Mar 22nd, 2022
811
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.09 KB | None | 0 0
  1. class Result {
  2.  
  3.     /*
  4.      * Complete the 'weightedUniformStrings' function below.
  5.      *
  6.      * The function is expected to return a STRING_ARRAY.
  7.      * The function accepts following parameters:
  8.      *  1. STRING s
  9.      *  2. INTEGER_ARRAY queries
  10.      */
  11.  
  12.     public static List<String> weightedUniformStrings(String s, List<Integer> queries) {
  13.     // Write your code here
  14.     List<String> result = new ArrayList<>();
  15.     //List<Integer> weights = new ArrayList<>();
  16.     //List<Integer> charValues = s.chars().mapToObj(c -> (int)(char)c-96).collect(Collectors.toList());
  17.     HashSet<Integer> weights = new HashSet<>();
  18.     char[] letters = s.toCharArray();
  19.     int amount = 1;
  20.     int weight;
  21.     for (int i = 0; i < letters.length; i++){
  22.         weight = letters[i]-96;
  23.         weights.add(weight*amount);
  24.         try{
  25.             if (Character.compare(letters[i], letters[i+1])  == 0){
  26.                 amount++;
  27.             }
  28.             else {
  29.                 amount = 1;
  30.             }
  31.         } catch (ArrayIndexOutOfBoundsException e){}
  32.     }
  33.    
  34.     queries.forEach(i -> {
  35.         if (weights.contains(i)){
  36.             result.add("Yes");
  37.         } else {
  38.             result.add("No");
  39.         }
  40.     });
  41.    
  42.     /*for (Integer i : queries){
  43.         if (weights.contains(i)){
  44.             result.add("Yes");
  45.         } else {
  46.             result.add("No");
  47.         }
  48.     }*/
  49.    
  50.     /*List<Integer> weights = new ArrayList<>();
  51.     char[] letters = s.toCharArray();
  52.    
  53.    
  54.     for (int i = 0; i <letters.length; i++){
  55.         int weight = (int)letters[i]-96;
  56.         int amount = 1;
  57.         for (int j = i+1 ; j < letters.length; j++){
  58.             weights.add(weight*amount);
  59.             if (letters[j] != letters[i]){
  60.                 i = j-1;
  61.                 break;
  62.             }
  63.             amount++;
  64.         }
  65.         weights.add(weight*amount);
  66.     }
  67.    
  68.     for (Integer i : queries){
  69.         if (weights.contains(i)){
  70.             result.add("Yes");
  71.         } else {
  72.             result.add("No");
  73.         }
  74.     }*/
  75.    
  76.     return result;
  77.  
  78.     }
  79.  
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement