MatchResult start() Method in Java with Examples

Maruthi Krishna
Updated on 10-Jan-2020 10:24:36

102 Views

The java.util.regex.MatcheResult interface provides methods to retrieve the results of a match.You can get an object of this interface using the toMatchResult() method of the Matcher class. This method returns a MatchResult object which represents the match state of the current matcher.The start() method of this interface returns the beginning index of the current match.Example Live Demoimport java.util.Scanner; import java.util.regex.MatchResult; import java.util.regex.Matcher; import java.util.regex.Pattern; public class StartExample {    public static void main(String args[]) {       //Reading String from user       System.out.println("Enter a String");       Scanner sc = new Scanner(System.in);       String input = ... Read More

Character Class P in Java Regex

Maruthi Krishna
Updated on 10-Jan-2020 10:20:10

415 Views

This character class \p{javaMirrored} matches upper case letters. This class matches the characters which returns true when passed as a parameter to the isMirrored() method of the java.lang.Character class.Example 1 Live Demoimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample {    public static void main(String args[]) {       //Reading String from user       System.out.println("Enter a string");       Scanner sc = new Scanner(System.in);       String input = sc.nextLine();       //Regular expression       String regex = "[\p{javaMirrored}]";       //Compiling the regular expression       Pattern pattern = ... Read More

Character Class \p{JavaWhitespace} in Java Regex

Maruthi Krishna
Updated on 10-Jan-2020 10:09:24

477 Views

This character class \p{javaWhitespace} matches spaces. This class matches the characters which returns true when passed as a parameter to the isWhitespace() method of the java.lang.Character class.Example 1 Live Demoimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample {    public static void main(String args[]) {       //Reading String from user       System.out.println("Enter a string");       Scanner sc = new Scanner(System.in);       String input = sc.nextLine();       //Regular expression       String regex = "[\p{javaWhitespace}]";       //Compiling the regular expression       Pattern pattern = Pattern.compile(regex);   ... Read More

Character Class \p{javaUppercase} in Java Regex

Maruthi Krishna
Updated on 10-Jan-2020 10:05:40

704 Views

This character class \p{javaUpperCase} matches upper case letters. This class matches the characters which returns true when passed as a parameter to the isUpperCase() method of the java.lang.Character class.Example 1 Live Demoimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample {    public static void main(String args[]) {       //Reading String from user       System.out.println("Enter a string");       Scanner sc = new Scanner(System.in);       String input = sc.nextLine();       //Regular expression       String regex = "[\p{javaUpperCase}]";       //Compiling the regular expression       Pattern pattern = ... Read More

Character Class \p{javaLowerCase} in Java Regex

Maruthi Krishna
Updated on 10-Jan-2020 09:59:42

550 Views

This character class \p{javaLowerCase} matches lower case letters. This class matches the characters which returns true when passed as a parameter to the isLowerCase() method of the java.lang.Character class.Example 1 Live Demoimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample {    public static void main(String args[]) {       //Reading String from user       System.out.println("Enter a string");       Scanner sc = new Scanner(System.in);       String input = sc.nextLine();       //Regular expression       String regex = "[\p{javaLowerCase}]";       //Compiling the regular expression       Pattern pattern = ... Read More

Maximum or Sum of Sub-Arrays of Two Different Arrays in C++

Narendra Kumar
Updated on 10-Jan-2020 08:04:03

221 Views

Problem statementGiven two arrays of positive integers. Select two sub-arrays of equal size from each array and calculate maximum possible OR sum of the two sub-arrays.ExampleIf arr1[] = {1, 2, 4, 3, 2} andArr2[] = {1, 3, 3, 12, 2} then maximum result is obtained when we create following two subarrays −Subarr1[] = {2, 4, 3} andSubarr2[] = {3, 3, 12}AlgorithmWe can use below formula to gets result −f(a, 1, n) + f(b, 1, n)Example Live Demo#include using namespace std; int getMaximumSum(int *arr1, int *arr2, int n) {    int sum1 = 0;    int sum2 = 0;    for ... Read More

Maximum Number of Fixed Points Using At Most 1 Swap in C++

Narendra Kumar
Updated on 10-Jan-2020 07:58:37

206 Views

Problem statementGiven a permutation of N elements from 0 to N-1. A fixed point is an index at which the value is same as the index i.e. arr[i] = i. You are allowed to make at most 1 swap. Find the maximum number of fixed points that you can get.ExampleIf input array is {0, 1, 2, 3, 4, 6, 5} then answer is 7.To adjust fixed point, we have to swap 6 and 5After this entire array becomes fixed point and maximum value of fixed point is 7.AlgorithmCreate an array pos which keeps the position of each element in the ... Read More

Maximum Number of Edges in a Bipartite Graph in C++

Narendra Kumar
Updated on 10-Jan-2020 07:56:14

347 Views

Problem statementA tree is always a Bipartite Graph as we can always break into two disjoint sets with alternate levels.In other words, we always color it with two colors such that alternate levels have same color. The task is to compute the maximum no. of edges that can be added to the tree so that it remains Bipartite Graph.ExampleTree edges are represented in vertex pairs as follows −{1, 2}{1, 3}{2, 4}{3, 5} then we need 2 more edges to keep it Bipartite GraphIn a coloring graph the graph {1, 4, 5} and {2, 3} from two different sets. Since, 1 ... Read More

Maximum Number of Edges in Bipartite Graph in C++

Narendra Kumar
Updated on 10-Jan-2020 07:53:40

364 Views

Problem statementGiven an integer N which represents the number of Vertices. The Task is to find the maximum number of edges possible in a Bipartite graph of N vertices.Bipartite GraphA Bipartite graph is one which is having 2 sets of vertices.The set are such that the vertices in the same set will never share an edge between them.ExampleIf N = 10 then there will be total 25 edges −Both sets will contain 5 vertices and every vertex of first set will have an edge to every other vertex of the second setHence total edges will be 5 * 5 = ... Read More

Maximum Length of Segments of 0's and 1's in C++

Narendra Kumar
Updated on 10-Jan-2020 07:40:21

264 Views

Problem statementGiven a string comprising of ones and zeros. The task is to find the maximum length of the segments of string such that a number of 1 in each segment is greater than 0ExampleIf input string is “10111000001011” the answer will 12 as follows −First segment is of length 7 10111000001011Second segment is of length 5 10111000001011Total length is length of (segment 1 + segment 2) = (7 + 5) = 12AlgorithmIf start == n then return 0.Run a loop from start till n, computing for each subarray till n.If character is 1 then increment the count of 1 ... Read More

Advertisements