Posts

Showing posts with the label StringBuffer

Valid Palindrome LeetCode

 Approach 1: Using Inbuilt reverse() Function on StringBuffer Object.           s=s.replaceAll("[^a-zA-Z0-9]", "");//replace all non alphanumeric characters with empty string.         s=s.toLowerCase();                 StringBuffer sb=new StringBuffer();         sb.append(s);         String s2=sb.reverse().toString();         if(s.equals(s2)){             return true;         }         else{             return false;         } Time: O(N) [As do swapping of characters by iterating up to half of the length of  given string length.][LeetCode Time: 23 ms beats 30.26% ] Space: O(Length of String)[LeetCode Memory: 40MB beats 32.77%] ========================================================================= Approach 2: ...

Remove Consecutive Characters Geeks For Geeks

 class Solution{     public String removeConsecutiveCharacter(String s){         Stack<Character> stack=new Stack<Character>(); int count=0; for(int i=0;i<s.length();i++){ if(stack.isEmpty()==true){ stack.push(s.charAt(i)); } else if(String.valueOf(stack.peek()).equals(String.valueOf(s.charAt(i)))){ count++; } else stack.push(s.charAt(i)); } //System.out.println(stack); StringBuffer sb=new StringBuffer(); while(!stack.isEmpty()){     sb.append(stack.pop()); } sb.reverse(); return sb.toString();     } } APPROACH: Here we will use a stack. We will add the elements of string in the stack in such a manner that if an element is equal to the peek of the stack it won't get added to the stack otherwise it will be added in the stack. I have used a counter variable also which will count the number of times different duplicate elements were traversed. After...

Reverse A String Using Stack Geeks For Geeks

 class Solution {          public String reverse(String s){         Stack<Character> stack=new Stack<Character>(); for(int i=0;i<s.length();i++){ stack.push(s.charAt(i)); } StringBuffer sb=new StringBuffer(); while(!stack.isEmpty()){ sb.append(stack.pop()); } String s1=sb.toString(); return s1;     } } Approach: We will add all the elements of the given string into the stack. After that, we will apply a while loop which will run till the stack becomes empty and the object of StringBuffer will append those characters. In the end, we will convert the string buffer object to a string.  TIME: O(N) [here N is the length of given string][GFG time: 0.2/1.5] SPACE: O(1) [here we are simply appending the characters in the last and later converting object of StringBuffer into a string.] Thanks for Reading 😇.

Palindrome String Geeks For Geeks (2 Different Approaches)

First Approach  Using StringBuffer Reverse method Time : 0.3 sec / 11.1 sec   class Solution {     int isPlaindrome(String s) {          StringBuffer sb=new StringBuffer(); sb.append(s); String a=(sb.reverse()).toString(); if(a.equals(s)){ //System.out.println("True"); return 1; } else{ //System.out.println("False"); return 0; }                                             // code here     } }; ################################################################################## Another Approach  Comparing each character Time : 0.3 sec / 11.1 sec class Solution {     int isPlaindrome(String s) {                 int start=0; int count=0; int end=s.length()-1; if(s.length()>1){ while(start<end){ cha...

Reverse String LeetCode JAVA 2 Different Solutions

 public static void main(String arg[]){ Scanner sc=new Scanner(System.in); System.out.println("Enter total number of characters in string"); int n=sc.nextInt(); char arr[]=new char[n]; for(int i=0;i<n;i++){ arr[i]=sc.next().charAt(0); ; } for(int i=0;i<n;i++){ System.out.print(arr[i]+" "); } /* Approach 1 : Swapping elements till half. Leetcode stats: Runtime 112 seconds, Memory 42.4 MB.  int start=0; int end=arr.length-1; while(start<end){ char temp; temp=arr[start]; arr[start]=arr[end]; arr[end]=temp; start++; end--; } for(int i=0;i<n;i++){ System.out.print(arr[i]+" "); } */ //============================================================ /* Approach 2 : Using the StringBuffer reverse method. Leetcode stats: Runtime 114 seconds, Memory 41.8 MB.  String str = new String(arr); StringBuffer sb=new StringBu...