Skip to main content

Posts

Showing posts with the label String

Password checker problem

Problem :  Implement a function name checkPassword(String str, int len, int min). The function will accept a string str and its length len and another argument min which denotes minimum length of the password. You are supposed to print valid, if the password meets the criteria given below or invalid if the password doesn't meet the criteria mentioned below: - at least of min characters. - at least one numeric digit. - at least one capital letter. - at least one special character. - must not have space or + - starting character must not be a number.  Java Solution : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 import java.util.Scanner; public class Main { public static void main (String[] args) { Scanner sc = new Scanner(System. in ); String str = sc. next (); int min = sc. nextInt (); System. out . println (checkPassword(str, str. length (), min)); } public ...

How to sort a string array by 2nd char of each string in the array ?

 Given a string array. Write a code to sort the strings in the array based on their 2nd character. Assumption :-  Each array element contains more than one character. Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 public class SortString { public static void main (String[] args) { String[] arr = { "heist" , "code" , "blog" , "python" , "java" }; for ( int i= 0 ; i<arr. length - 1 ; i++){ for ( int j=i+ 1 ; j<arr. length ; j++){ if (arr[i]. charAt ( 1 )>arr[j]. charAt ( 1 )){ String temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } } for (String s:arr) System. out . println (s); } } Python  (using sort and lambda) arr = [ "heist" , "code" , "blog" , "python" , ...

Check If a Word Occurs As a Prefix of Any Word in a Sentence

 Given a sentence that consists of some words separated by a single space, and a searchWord. You have to check if searchWord is a prefix of any word in sentence. Return the index of the word in the sentence where searchWord is the prefix of this word. If search word is the prefix of more than word, return the index of first word. If there is no such word return -1. Ex:- Input:  sentence= "I love burger"  searchWord = "burg" Output:  2 Ex:- Input:  sentence= "I am tired"  searchWord = "you" Output:   -1 Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 public class MyClass { public static void main (String args[]) { String sentence = "this problem is an easy problem" ; String searchWord = "pro" ; System. out . println (findIndex(sentence, searchWord)); } public static int findIndex (String sentence, String searchWord){ String[] arr = sentence. split (...

Longest Palindromic Substring

 Ex:- If the given string is "codedoh" Output:-  "odedo" is the longest palindromic substring Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 public String longestPalindrome (String s) { if (s == null || s. length () < 1 ) return "" ; int start = 0 , end = 0 ; for ( int i = 0 ; i < s. length (); i++) { int len1 = expandAroundCenter(s, i, i); int len2 = expandAroundCenter(s, i, i + 1 ); int len = Math. max (len1, len2); if (len > end - start) { start = i - (len - 1 ) / 2 ; end = i + len / 2 ; } } return s. substring (start, end + 1 ); } private int expandAroundCenter (String s, int left, int right) { int L = left, R = right; while (L >= 0 && R < s. length () && s. charAt (L) == s. charAt (R)) { ...

Reverse each word of sentence without changing their order

   Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 import java.util.Scanner; public class RevWordSen{ public static void main (String []args){ Scanner sc = new Scanner(System. in ); String sentence = sc. nextLine (); String[] arr = sentence. split ( " " ); for ( int i= 0 ; i<=arr. length - 1 ; i++) arr[i] = reverse(arr[i]); String result = String. join ( " " ,arr); System. out . println (result); } public static String reverse (String str){ String rev = "" ; for ( int i=str. length ()- 1 ; i>= 0 ; i--) rev += str. charAt (i) ; return rev; } }  Python sentence = input ( "Enter a sentence" ); str_arr = list (sentence . split( " " )) result = "" for item in str_arr: result += item[:: - 1 ] + " "...

Print all possible rotations of a string

 Ex:- "code" ==>   "code"  "odec"  "deco"  "ecod"          "heist"  ==>   "heist"  "eisth"  "isthe"  "sthei"  "theis" Java 1 2 3 4 5 6 7 8 9 public class Rotation{ public static void main (String[] args){ String str = "codeheist" ; for ( int i= 0 ; i<str. length (); i++){ String ans = str. substring (i,str. length ()) + str. substring ( 0 ,i); System. out . println (ans); } } } Python input_str = "codeheist" for i in range ( 0 , len (input_str)): print ( input_str [ i : len (input_str) ] + input_str [ 0 : i ] )

Write a method to Mirror Encrypt a String

 Mirror Encrypt: a -- z       b -- y     c -- x    d -- w  ............... y -- b     z -- a A -- Z      B --- Y   C -- X   D -- W  ................ Y -- B     Z -- A EX:-    abcd ==> zyxw              AnT   ==> ZmG Ternary or conditional Operator Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 public class MirrorString{ public static void main (String[] args){ String str = "codeheist" ; System. out . println (mirrorEncrypt(str)); } public static String mirrorEncrypt (String str){ String ans = "" ; for ( int i= 0 ; i<str. length (); i++){ char ch = str. charAt (i); ans += Character. isUpperCase (ch) ? ( char )( 'Z' + 'A' -ch) : ( char )( 'z' + 'a' -ch); } ...

Char sum

                         Java 1 2 3 4 5 6 7 8 9 10 11 12 13 import java.util.Scanner; public class CharSum{ public static void main (String []args){ Scanner sc = new Scanner(System. in ); System. out . println ( "Enter a string:" ); String str = sc. next (); int sum = 0 ; for ( int i= 0 ; i<str. length (); i++){ sum += (( int )str. charAt (i) - 96 ); } System. out . print ( "Char sum: " + sum); } } Python input_str = input ( "Enter a string:" ) sum = 0 for char in input_str: sum += ord (char) - 96 print ( "char sum : " , sum )

Write a function to check for valid parenthesis (balanced parenthesis)

Algorithm:-  The basic idea is to push the right parentheses ), }, ]  into the stack each time when we encounter left ones. And if a right bracket appears in the string, we need check if the stack is empty and also whether the top element is the same with that right bracket. If not, the string is not a valid one. At last, we also need check if the stack is empty. C++ 1 2 3 4 5 6 7 8 9 10 11 12 13 bool isValid(string s) { stack< char > st; //taking stack for keep tracking the order of the brackets.. for ( auto i:s) //iterate over each and every elements { if (i== '(' or i== '{' or i== '[' ) st.push(i); //if current element of the string will be opening bracket then we will just simply push it into the stack else //if control comes to else part, it means that current element is a closing bracket, so check two conditions current element matches with top of the stack and the st...

Write a function to replace all occurrence of a character with another character and vice versa in a string

 Implement the function to modify and return the string ‘ str’ in such a way that all occurrences of ‘ch1’ in the original string are replaced by ‘ch2’ and all occurrences of ‘ch2’  in the original string are replaced by ‘ch1’. Assumption:  String Contains only lower-case alphabetical letters. Note: Return null if the string is null or None. Java  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 public static String replaceCharacters (String str, char ch1, char ch2){ if (str== null ) return null ; else { String result = "" ; for ( int i= 0 ; i<str. length ; i++){ char ch = str. charAt (i); if (ch==ch1) result += ch2; else if (ch==ch2) result += ch1; else result += ch; } return result; ...

Write a method to move hyphens to the left and characters to the right in a string

 Ex:-     code--heist--   ==>     ----codeheist Note:-  Return null or None if str is empty. Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 public static String moveHyphens (String str) { if (str. length ()== 0 ) return null ; else { String result = "" ; for ( int i= 0 ; i<str. length (); i++){ char ch = str. charAt (i); if (ch== '-' ) result = ch + result; else result += ch; } return result; } } Python def moveHyphens (str): if len(str)== 0 : return None else : result = "" for char in str: if char== '-' : result = char + result; else : result += char return result

Separate digits, characters and special characters from a mixed string

 Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 import java.util.Scanner; public class Separate{ public static void main (String []args){ Scanner sc = new Scanner(System. in ); System. out . println ( "Enter a mixed string:" ); String mixed = sc. next (); String chars= "" , digits= "" , spec_chars= "" ; for ( int i= 0 ; i<mixed. length (); i++){ char ch = mixed. charAt (i); if (ch>= 'a' && ch<= 'z' || ch>= 'A' && ch<= 'Z' ) chars += ch; else if (ch>= '0' && ch<= '9' ) digits += ch; else spec_chars += ch; } System. out . println ( "Characters: " +char...

Write a program to reverse a string

 Ex:-  "code"  ==> "edoc"    ,    "heist" ==> "tsieh"  Java Python