From b2894b4bea9784ea2007b03f2e103221ec361577 Mon Sep 17 00:00:00 2001 From: RUDHRESH S <118143270+iamrudhresh@users.noreply.github.com> Date: Fri, 1 Nov 2024 11:56:37 +0530 Subject: [PATCH] Time: 38 ms (43.02%) | Memory: 45.7 MB (34.07%) - LeetSync --- ...elete-characters-to-make-fancy-string.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 1302-delete-characters-to-make-fancy-string/delete-characters-to-make-fancy-string.java diff --git a/1302-delete-characters-to-make-fancy-string/delete-characters-to-make-fancy-string.java b/1302-delete-characters-to-make-fancy-string/delete-characters-to-make-fancy-string.java new file mode 100644 index 0000000..413a946 --- /dev/null +++ b/1302-delete-characters-to-make-fancy-string/delete-characters-to-make-fancy-string.java @@ -0,0 +1,19 @@ +class Solution { + public String makeFancyString(String s) { + StringBuilder ans = new StringBuilder(); + ans.append(s.charAt(0)); + int n = s.length(), cnt = 1; + for (int i = 1; i < n; i++) { + if (s.charAt(i) == ans.charAt(ans.length() - 1)) { + cnt++; + if (cnt < 3) { + ans.append(s.charAt(i)); + } + } else { + cnt = 1; + ans.append(s.charAt(i)); + } + } + return ans.toString(); + } +} \ No newline at end of file