Java StringBuilder.setCharAt Examples

Java StringBuilder.setCharAt - 30 examples found. These are the top rated real world Java examples of StringBuilder.setCharAt extracted from open source projects. You can rate examples to help us improve the quality of examples.
Example #1
0
 public static void main(String[] args) {
   Scanner scanner = new Scanner(System.in);
   int m = scanner.nextInt();
   assert 1 <= m && m <= 100 : "out of range, m: " + m;
   int s = scanner.nextInt();
   assert 0 <= s && s <= 900 : "out of range, s: " + s;
   if (s > 9 * m || (s == 0 && m > 1)) {
     System.out.println("-1 -1");
     return;
   }
   if (m == 1 && s == 0) {
     System.out.println("0 0");
     return;
   }
   StringBuilder sb = new StringBuilder();
   int l = 0;
   for (int i = 0; i < m; i++) {
     int d = (s >= 9) ? 9 : s;
     sb.append(d);
     s -= d;
     if (d != 0) {
       l = i;
     }
   }
   String large = sb.toString();
   if (sb.charAt(m - 1) == '0') {
     sb.setCharAt(l, (char) (sb.charAt(l) - 1));
     sb.setCharAt(m - 1, '1');
   }
   String small = sb.reverse().toString();
   System.out.printf("%s %s", small, large);
 }
Example #2
0
  public String encrypt(String input, int key) {
    StringBuilder encrypted = new StringBuilder(input);

    String shiftedAlphabetUp = alphabetUp.substring(key) + alphabetUp.substring(0, key);
    String shiftedAlphabetLow = alphabetLow.substring(key) + alphabetLow.substring(0, key);

    for (int i = 0; i < encrypted.length(); i++) {
      char currChar = encrypted.charAt(i);

      if (alphabetUp.indexOf(currChar) != -1) {
        int idx = alphabetUp.indexOf(currChar);

        if (idx != -1) {
          char newChar = shiftedAlphabetUp.charAt(idx);

          encrypted.setCharAt(i, newChar);
        }
      } else if (alphabetLow.indexOf(currChar) != -1) {
        int idx = alphabetLow.indexOf(currChar);

        if (idx != -1) {
          char newChar = shiftedAlphabetLow.charAt(idx);

          encrypted.setCharAt(i, newChar);
        }
      }
    }
    return encrypted.toString();
  }
Example #3
0
 /*
  * This problem contraints the word to be all lower-case character, which most likely means it requires
  * you to do stupid constant(26) time operations to the strings. Doing a graph construction, which is
  * more general, got Time Limit Exception because it is O(n^2) rather than O((26*n)^2).
  */
 public static int ladderLength(String beginWord, String endWord, Set<String> wordDict) {
   Queue<String> queue = new LinkedList<String>();
   queue.add(beginWord);
   wordDict.remove(beginWord);
   int ans = 1;
   while (!queue.isEmpty()) {
     for (int count = queue.size(); count > 0; --count) {
       String word = queue.poll();
       StringBuilder altWord = new StringBuilder(word);
       for (int i = 0; i < word.length(); ++i) {
         char w = altWord.charAt(i);
         for (char c = 'a'; c <= 'z'; ++c) {
           if (w == c) continue;
           altWord.setCharAt(i, c);
           if (altWord.toString().equals(endWord)) return ans + 1;
           if (wordDict.contains(altWord.toString())) {
             queue.add(altWord.toString());
             wordDict.remove(altWord.toString());
           }
           altWord.setCharAt(i, w);
         }
       }
     }
     ++ans;
   }
   return 0;
 }
 @TCB
 private static void stripBannedCodeunits(StringBuilder sb, int start) {
   int k = start;
   for (int i = start, n = sb.length(); i < n; ++i) {
     char ch = sb.charAt(i);
     if (ch < 0x20) {
       if (IS_BANNED_ASCII[ch]) {
         continue;
       }
     } else if (0xd800 <= ch) {
       if (ch <= 0xdfff) {
         if (i + 1 < n) {
           char next = sb.charAt(i + 1);
           if (Character.isSurrogatePair(ch, next)) {
             sb.setCharAt(k++, ch);
             sb.setCharAt(k++, next);
             ++i;
           }
         }
         continue;
       } else if ((ch & 0xfffe) == 0xfffe) {
         continue;
       }
     }
     sb.setCharAt(k++, ch);
   }
   sb.setLength(k);
 }
Example #5
0
 public static String initcap(Comparable<?> param) throws ParseException {
   if (param == null) {
     return null;
   }
   String inputStr = param.toString();
   StringBuilder output = new StringBuilder(inputStr);
   boolean needUpper = true;
   final int len = inputStr.length();
   for (int i = 0; i < len; i++) {
     char c = inputStr.charAt(i);
     if (needUpper && Character.isLetter(c)) {
       if (Character.isLowerCase(c)) {
         output.setCharAt(i, Character.toUpperCase(c));
       }
       needUpper = false;
     } else if (!needUpper) {
       if (!Character.isLetterOrDigit(c)) {
         needUpper = true;
       } else if (Character.isUpperCase(c)) {
         output.setCharAt(i, Character.toLowerCase(c));
       }
     }
   }
   return output.toString();
 }
  @Override
  public String prepareMessage(String string) {
    String[] commands = string.split(";");
    StringBuilder message = new StringBuilder("00000");

    for (String command : commands) {
      try {
        switch (Integer.parseInt(command)) {
          case 0:
            message.setCharAt(0, '1');
            break;
          case 1:
            message.setCharAt(1, '1');
            break;
          case 2:
            message.setCharAt(2, '1');
            break;
          case 3:
            message.setCharAt(3, '1');
            break;
          case 4:
            message.setCharAt(4, '1');
            break;
          default:
            break;
        }
      } catch (NumberFormatException e) {
        return message.toString();
      }
    }

    return message.toString();
  }