From cc43f19ddd3ef99bcbb6ff58ee7daae5e0215084 Mon Sep 17 00:00:00 2001 From: Nasareen Yaligar Date: Mon, 5 Oct 2020 23:38:10 +0530 Subject: [PATCH] HACTOBER2020 String Algorithms --- StringAlgorithms/RunLength.class | Bin 0 -> 1458 bytes StringAlgorithms/RunLength.java | 34 +++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 StringAlgorithms/RunLength.class create mode 100644 StringAlgorithms/RunLength.java diff --git a/StringAlgorithms/RunLength.class b/StringAlgorithms/RunLength.class new file mode 100644 index 0000000000000000000000000000000000000000..3717b88e9e03c9c450cc0d241cf5c4e90edeed37 GIT binary patch literal 1458 zcmZ`(T~ixn6n@^Yn@!T?E0B`3V7FCk(oo_@MGFW57D)}1HZ91gGhMRtp$GPNcEz>5X64q-1K9`#l5>vdg!2M@#nPsL zw%p#88T27-LPt_yu;zGjz1!N6{#|>=l>*sXyJ5SVw(qF>kx&cwoIqf__WBHFiQcju zPvC6vtJ;f9VvpIv zU8#DVZb(#Vw`MaKLP3>sP9R@A)ugnkB8;1uz#F8Y-6bn8z&1$eGq4mafx?C>?Lb=6 z3#D&4DwY*Qv}bS$Zy9(~U?eJnsT}h*@is0C7##)Zs)UcyiJs#*2`b^Di4v{|q&)dB zRQwB!7E327GcYMI_9~N0UB}&(ehO1$GvdOE(Jd#Ku`(#*T}8-cb$Z{#2e`&Ausa>; z?Fw8yEvXZck3x{$GNw&@6eSzB<1iUq$E<;wm)|R5$iy5zVKg^t$*AY53L~w2J~gq3 zC2AXcw!g?n(2CX4vQmGhvb@3WDlVU7;dm~eE5lV~Sbe=Afo;FZW*s@D3;94n+;-H* zbsz6V_OKp3xf+94iKkMm^Q*EWgB^Ct35#dB(#T<Nxg5f+vnCSA(|-cfno1a)8wDkvhXwseu&Ea5ZBC?)|)_ zF@Qlz=q$d57J>Zy1Vh`8kv%}Z_88~07kao@?%|y^UY?-5oxOU1%484k_3$B8JbgT` zR7=r*M8P9z=0h^o})5_ET4RYeKm?DEPV@i( literal 0 HcmV?d00001 diff --git a/StringAlgorithms/RunLength.java b/StringAlgorithms/RunLength.java new file mode 100644 index 0000000..42de3a7 --- /dev/null +++ b/StringAlgorithms/RunLength.java @@ -0,0 +1,34 @@ +package StringAlgorithms; + +import java.util.Scanner; + +/* + * Runlength - given aabbbaa converts to a2b3a2. + */ +public class RunLength { + + public static void main(String[] args) { + + @SuppressWarnings("resource") + Scanner scanner = new Scanner(System.in); + System.out.println("Please enter input string"); + String input = scanner.nextLine(); + System.out.println("Input String is: " + input); + + int N = input.length(); + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < N; i++) { + int runLen = 1; + while (i + 1 < N && input.charAt(i) == input.charAt(i + 1)) { + runLen++; + i++; + } + + sb.append(input.charAt(i)); + sb.append(runLen); + } + + System.out.println(sb.toString()); + } + +}