Wednesday, December 6, 2017

Strings Manipulation Algorithms











find the longest substring with at most k repeating characters,
to make it more clear, at most k repeating characters means in the substring, the max count(frequency) of one or more chars is k
-->








1)/*word break problem*/



2)/*Permute  String*/




3)/*Grouping Anagrams */



4)/*sort by last name : comaparator in java 8 */


P.S the following has been picked up from internet & is not original work 


5)/* Find if a word  is present in a sentence */



 public static boolean searchString(String find){  
          return ("Yellow Banana,Green Apple red Apple ,Oranges ".indexOf(find)== -1)?false:true;  
      }  


6)/*  Check Panagram */




 Working on optimal solution for

7)/* Check Anagrams */


Preview:
 public static void checkDigits(){
  String first = "Morning10";
  //Pattern pattern = Pattern.compile(".*\\D.*");
  Pattern pattern = Pattern.compile(".*[^0-9].*");
  Matcher matcher = pattern.matcher(first);
  if(matcher.matches()) System.out.println("yes there are digits present !");
 }

8)/* Find first non repeating character*/

Solution 1

Preview:
 public static void nonRepeatingChar(){  
           String first = "Morning";  
           for(int i=0;i
           String tempStr = 
           first.substring(0, i)+first.substring(i+1,first.length());  
                     if(tempStr.indexOf(first.charAt(i)) == -1 )  
                          System.out.println("The non repeating character is :"+first.charAt(i));  
                }  
           }  


Solution 2
-


-


9)/* Given two strings s1, s2, write a function that returns true if s2 is a special substring s1. 

A special substring s2 is such that the s1 contains s2 where each character in s2 appears in sequence in s1, but there can be any characters in s1 in between the sequence.

 Example: 
 isSpecialSubstring('abcdefg', 'abc') => true 
 isSpecialSubstring('abcdefg', 'acg') => true 
 isSpecialSubstring('abcdefg', 'acb') => false; 

 The first two are abc and acg appears in 'abcdefg' in that order, although there might be multiple chars between the next character in s2. The last one is false, because in 'acb' the character 'b' appears before the character 'c' in 'abcdefg' Hope thats clear. * */



 public class SpecialString {  

      public static void main(String[] args) {  
           System.out.println("Here is my the answer case 1:"+isSpecialSubstring("abcdefg", "abc"));  
           System.out.println("Here is my the answer case 2:"+isSpecialSubstring("abcdefg", "acg"));   
           System.out.println("Here is my the answer case 3:"+isSpecialSubstring("abcdefg", "acb"));   
      }  
      public static boolean isSpecialSubstring(String original, String theSubString){            
           if(original.indexOf(theSubString, 0) !=-1){      return true;  
           }else{  
                String order = "";  
                for(int i =0;i<original.length();i++){  
                     String c = String.valueOf(original.charAt(i));  
                     if(theSubString.contains(c)) order = order.concat(c);  
                }  
                if(order.indexOf(theSubString, 0) !=-1)return true;  
           }            
           return false; }  
 }  


10)/*Max ocurring character in String */

      public static void maxOccuringCharacter() {  
           String str = "Red roses blue sky green plants brown earth let's conserve";  
           TreeMap map = new TreeMap();  
           for(int i =0;i         NavigableMap nmap=map.descendingMap();  
           //NavigableMap nmap=map.subMap(fromKey, toKey)  
           //NavigableMap nmap=map.headMap(toKey, inclusive)  
        //NavigableMap nmap=map.tailMap(fromKey, inclusive)  
             for(Character ch : map.keySet()){  
                   int c = map.get(ch);  
                      System.out.println("Character is :"+ ch +" Count is :"+c);   
                     // break;  
             }  
      }  





11) 

12)
13)
14)

https://codingsec.net/2016/07/skills-companies-like-google-expect-software-engineers/







No comments:

Post a Comment