@Test public void testEncodeRepeatedChars() { assertEquals("a3b2", encodeRepeatedChars("aaabb")); assertEquals("abc", encodeRepeatedChars("abc")); assertEquals("a3b3a1", encodeRepeatedChars("aaabbba")); assertEquals("a1b3c3", encodeRepeatedChars("abbbccc")); }
public static String encodeRepeatedChars(final String str) { final AtomicInteger repeatCount = new AtomicInteger(1); final StringBuilder previousChar = new StringBuilder(" "); final StringBuilder buffer = new StringBuilder(); final int len = str.length() - 1; IntStream.range(0, str.length()).forEach(idx -> { if (str.charAt(idx) == previousChar.charAt(0)) { repeatCount.incrementAndGet(); } else { if (idx != 0) { buffer.append(previousChar.charAt(0)).append(repeatCount.get()); } repeatCount.set(1); previousChar.setCharAt(0, str.charAt(idx)); } if (idx == len) { buffer.append(str.charAt(idx)).append(repeatCount.get()); } }); return buffer.length() < str.length() ? buffer.toString() : str; }
PracticeQuestionsCh1 { public static String encodeRepeatedChars(final String str) { final AtomicInteger repeatCount = new AtomicInteger(1); final StringBuilder previousChar = new StringBuilder(" "); final StringBuilder buffer = new StringBuilder(); final int len = str.length() - 1; IntStream.range(0, str.length()).forEach(idx -> { if (str.charAt(idx) == previousChar.charAt(0)) { repeatCount.incrementAndGet(); } else { if (idx != 0) { buffer.append(previousChar.charAt(0)).append(repeatCount.get()); } repeatCount.set(1); previousChar.setCharAt(0, str.charAt(idx)); } if (idx == len) { buffer.append(str.charAt(idx)).append(repeatCount.get()); } }); return buffer.length() < str.length() ? buffer.toString() : str; } }
PracticeQuestionsCh1 { public static String encodeRepeatedChars(final String str) { final AtomicInteger repeatCount = new AtomicInteger(1); final StringBuilder previousChar = new StringBuilder(" "); final StringBuilder buffer = new StringBuilder(); final int len = str.length() - 1; IntStream.range(0, str.length()).forEach(idx -> { if (str.charAt(idx) == previousChar.charAt(0)) { repeatCount.incrementAndGet(); } else { if (idx != 0) { buffer.append(previousChar.charAt(0)).append(repeatCount.get()); } repeatCount.set(1); previousChar.setCharAt(0, str.charAt(idx)); } if (idx == len) { buffer.append(str.charAt(idx)).append(repeatCount.get()); } }); return buffer.length() < str.length() ? buffer.toString() : str; }  }
PracticeQuestionsCh1 { public static String encodeRepeatedChars(final String str) { final AtomicInteger repeatCount = new AtomicInteger(1); final StringBuilder previousChar = new StringBuilder(" "); final StringBuilder buffer = new StringBuilder(); final int len = str.length() - 1; IntStream.range(0, str.length()).forEach(idx -> { if (str.charAt(idx) == previousChar.charAt(0)) { repeatCount.incrementAndGet(); } else { if (idx != 0) { buffer.append(previousChar.charAt(0)).append(repeatCount.get()); } repeatCount.set(1); previousChar.setCharAt(0, str.charAt(idx)); } if (idx == len) { buffer.append(str.charAt(idx)).append(repeatCount.get()); } }); return buffer.length() < str.length() ? buffer.toString() : str; }  static boolean isUnique(String s); static boolean isPermutation(String s1, String s2); static String encodeRepeatedChars(final String str); static void fillIfZero(int[][] matrix); static boolean isRotation(String s1, String s2); }
PracticeQuestionsCh1 { public static String encodeRepeatedChars(final String str) { final AtomicInteger repeatCount = new AtomicInteger(1); final StringBuilder previousChar = new StringBuilder(" "); final StringBuilder buffer = new StringBuilder(); final int len = str.length() - 1; IntStream.range(0, str.length()).forEach(idx -> { if (str.charAt(idx) == previousChar.charAt(0)) { repeatCount.incrementAndGet(); } else { if (idx != 0) { buffer.append(previousChar.charAt(0)).append(repeatCount.get()); } repeatCount.set(1); previousChar.setCharAt(0, str.charAt(idx)); } if (idx == len) { buffer.append(str.charAt(idx)).append(repeatCount.get()); } }); return buffer.length() < str.length() ? buffer.toString() : str; }  static boolean isUnique(String s); static boolean isPermutation(String s1, String s2); static String encodeRepeatedChars(final String str); static void fillIfZero(int[][] matrix); static boolean isRotation(String s1, String s2); static final Logger LOGGER; }