Posts

Showing posts from March, 2022

Index of first repeated character in a string.

Let me break down and elaborate on the Java code snippet you've provided. This code is designed to find the first repeated character in a string and then print its index. Code Explanation 1. String Initialization java Copy code String repeadted = "adffda" ; A string repeadted is initialized with the value "adffda" . The word seems like a typo, and it probably meant "repeated." 2. Creating a Set to Track Characters java Copy code Set<Character> set1 = new HashSet <>(); A HashSet named set1 is created to store characters from the string repeadted . The HashSet data structure is chosen because it does not allow duplicate elements, making it perfect for detecting repeated characters. 3. Finding the First Repeated Character java Copy code char ch1 ...

First Repeating character in a string

Please read the post for explanation: Index of first repeated character in a string. System.out.println("First repeating character in a string"); Set set=new HashSet (); "adffda".chars() .filter(c->!set.add((char)c)) .findFirst() .ifPresent(ch->System.out.print((char)ch));

Reverse a String using Recursion

class ReverseAStringInRecusive {          public static String result="";          public static void main(String[] args) {         reverseString("inayathulla");         System.out.println(result);     }          public static String reverseString(String origin){          if(origin.length()>0){          result=result+origin.charAt(origin.length()-1);          return reverseString(origin.substring(0,origin.length()-1));          }          return origin;         }              } =