Skip to content

Commit f135e63

Browse files
committed
Add solutions of Java Strings problems
1 parent ed69f01 commit f135e63

12 files changed

Lines changed: 168 additions & 148 deletions

Java/Strings/JavaAnagrams.java

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,24 @@
1-
import java.io.*;
2-
import java.util.*;
3-
4-
public class JavaAnagrams {
5-
61
static boolean isAnagram(String a, String b) {
7-
int s1[] = new int[26];
8-
int s2[] = new int[26];
9-
int l1 = a.length();
10-
int l2 = b.length();
11-
if (l1 != l2)
2+
int s1[]=new int[26];
3+
int s2[]=new int[26];
4+
int l1=a.length();
5+
int l2=b.length();
6+
if(l1!=l2)
127
return false;
138
int n;
14-
for (int i = 0; i < l1; i++) {
15-
n = a.charAt(i) - 65;
16-
if (n > 25)
17-
n -= 32;
9+
for(int i=0;i<l1;i++){
10+
n=a.charAt(i)-65;
11+
if(n>25)
12+
n-=32;
1813
s1[n]++;
19-
n = b.charAt(i) - 65;
20-
if (n > 25)
21-
n -= 32;
14+
n=b.charAt(i)-65;
15+
if(n>25)
16+
n-=32;
2217
s2[n]++;
2318
}
24-
for (int i = 0; i < 26; i++) {
25-
if (s1[i] != s2[i])
19+
for(int i=0;i<26;i++){
20+
if(s1[i]!=s2[i])
2621
return false;
2722
}
2823
return true;
29-
}
30-
31-
public static void main(String[] args) {
32-
Scanner scan = new Scanner(System.in);
33-
String a = scan.next();
34-
String b = scan.next();
35-
scan.close();
36-
boolean ret = isAnagram(a, b);
37-
System.out.println((ret) ? "Anagrams" : "Not Anagrams");
38-
}
39-
}
24+
}

Java/Strings/JavaRegex.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
class JavaRegex {
2-
String pattern = "^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
3-
+ "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])$";
1+
class myRegex
2+
{
3+
String pattern =
4+
"^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
5+
"([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
6+
"([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
7+
"([01]?\\d\\d?|2[0-4]\\d|25[0-5])$";
8+
49
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
import java.util.Scanner;
3+
import java.util.regex.Matcher;
4+
import java.util.regex.Pattern;
5+
6+
public class DuplicateWords
7+
{
8+
public static void main(String[] args){
9+
10+
String pattern = "\\b(\\w+)(\\b\\W+\\b\\1\\b)*";
11+
Pattern r = Pattern.compile(pattern, Pattern.MULTILINE+Pattern.CASE_INSENSITIVE);
12+
13+
Scanner in = new Scanner(System.in);
14+
int testCases = Integer.parseInt(in.nextLine());
15+
while(testCases>0){
16+
String input = in.nextLine();
17+
Matcher m = r.matcher(input);
18+
boolean findMatch = true;
19+
while(m.find( )){
20+
input = input.replaceAll(m.group(),m.group(1));
21+
findMatch = false;
22+
}
23+
System.out.println(input);
24+
testCases--;
25+
}
26+
}
27+
}

Java/Strings/JavaRegex2DuplicateWords.java

Lines changed: 0 additions & 26 deletions
This file was deleted.
Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
import java.io.*;
22
import java.util.*;
33

4-
public class JavaStringReverse {
4+
public class Solution {
55

66
public static void main(String[] args) {
7-
8-
Scanner sc = new Scanner(System.in);
9-
String A = sc.next();
7+
8+
Scanner sc=new Scanner(System.in);
9+
String A=sc.next();
1010
int l = A.length();
1111
boolean ispallin = true;
12-
for (int i = 0; i < l / 2; i++) {
13-
if (A.charAt(i) != A.charAt(l - 1 - i))
14-
ispallin = false;
12+
for(int i=0;i<l/2;i++){
13+
if(A.charAt(i)!=A.charAt(l-1-i))
14+
ispallin=false;
1515
}
16-
System.out.println(ispallin ? "Yes" : "No");
16+
if(ispallin)
17+
System.out.println("Yes");
18+
else
19+
System.out.println("No");
1720
}
1821
}

Java/Strings/JavaStringTokens.java

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
1-
import java.io.*;
2-
import java.util.*;
1+
import java.io.*;
2+
import java.util.*;
33

4-
public class JavaStringTokens {
54

6-
public static void main(String[] args) {
75

8-
Scanner scan = new Scanner(System.in);
9-
String s = scan.nextLine();
10-
scan.close();
6+
public class Solution {
117

12-
// Complete the code
13-
StringTokenizer st = new StringTokenizer(s, " !,?._'@");
14-
System.out.println(st.countTokens());
15-
while (st.hasMoreTokens()) {
16-
System.out.println(st.nextToken());
8+
9+
public static void main(String[] args)
10+
{
11+
12+
13+
Scanner scan = new Scanner(System.in);
14+
String s=scan.nextLine();
15+
//Complete the code
16+
StringTokenizer st = new StringTokenizer(s," !,?._'@");
17+
System.out.println(st.countTokens());
18+
while(st.hasMoreTokens()){
19+
System.out.println(st.nextToken());
20+
}
1721
}
1822
}
19-
}
Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
import java.io.*;
22
import java.util.*;
33

4-
public class JavaStringsIntroduction {
4+
public class Solution {
55

66
public static void main(String[] args) {
7-
8-
Scanner sc = new Scanner(System.in);
9-
String A = sc.next();
10-
String B = sc.next();
7+
8+
Scanner sc=new Scanner(System.in);
9+
String A=sc.next();
10+
String B=sc.next();
1111
System.out.println(A.length() + B.length());
12-
System.out.println((A.compareTo(B) > 0) ? "Yes" : "No");
13-
System.out.println(Character.toUpperCase(A.charAt(0)) + A.substring(1) + " "
14-
+ Character.toUpperCase(B.charAt(0)) + B.substring(1));
15-
16-
sc.close();
12+
if(A.compareTo(B)>0)
13+
System.out.println("Yes");
14+
else
15+
System.out.println("No");
16+
System.out.println(Character.toUpperCase(A.charAt(0)) + A.substring(1) +
17+
" " + Character.toUpperCase(B.charAt(0)) + B.substring(1));
18+
1719
}
1820
}

Java/Strings/JavaSubstring.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import java.io.*;
22
import java.util.*;
3+
import java.text.*;
4+
import java.math.*;
5+
import java.util.regex.*;
36

4-
public class JavaSubstring {
7+
public class Solution {
58

69
public static void main(String[] args) {
710
Scanner in = new Scanner(System.in);
811
String S = in.next();
912
int start = in.nextInt();
1013
int end = in.nextInt();
11-
System.out.println(S.substring(start, end));
12-
13-
in.close();
14+
System.out.println(S.substring(start,end));
1415
}
1516
}
Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
11
import java.io.*;
22
import java.util.*;
33

4-
public class JavaSubstringComparisons {
4+
public class Solution {
55

6-
public static String getSmallestAndLargest(String s, int k) {
7-
int n = s.length() - k;
8-
String[] arr = new String[n + 1];
9-
for (int i = 0; i <= s.length() - k; i++) {
10-
arr[i] = s.substring(i, i + k);
6+
public static void main(String[] args) {
7+
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
8+
9+
String str;
10+
Scanner sc=new Scanner(System.in);
11+
12+
str=sc.nextLine();
13+
int k;
14+
k=sc.nextInt();
15+
int n=str.length()-k;
16+
String[] arr = new String[n+1];
17+
for(int i=0;i<=str.length()-k;i++)
18+
{
19+
arr[i]=str.substring(i,i+k);
20+
1121
}
1222
Arrays.sort(arr);
13-
return arr[0] + "\n" + arr[n];
14-
}
15-
16-
public static void main(String[] args) {
17-
Scanner sc = new Scanner(System.in);
18-
String s = sc.nextLine();
19-
int k = sc.nextInt();
20-
sc.close();
21-
22-
System.out.println(getSmallestAndLargest(s, k));
23+
System.out.println(arr[0]);
24+
System.out.println(arr[n]);
25+
2326
}
2427
}

Java/Strings/PatternSyntaxChecker.java

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,21 @@
22
import java.util.regex.*;
33
import java.util.*;
44

5-
public class PatternSyntaxChecker {
6-
public static void main(String[] args) {
7-
Scanner in = new Scanner(System.in);
8-
int testCases = Integer.parseInt(in.nextLine());
9-
while (testCases > 0) {
10-
String pattern = in.nextLine();
11-
try {
12-
Pattern.compile(pattern);
13-
System.out.println("Valid");
14-
} catch (Exception e) {
15-
System.out.println("Invalid");
16-
}
17-
testCases--;
18-
}
19-
}
5+
public class Solution
6+
{
7+
public static void main(String[] args){
8+
Scanner in = new Scanner(System.in);
9+
int testCases = Integer.parseInt(in.nextLine());
10+
while(testCases>0){
11+
String pattern = in.nextLine();
12+
try{
13+
Pattern.compile(pattern);
14+
System.out.println("Valid");
15+
}
16+
catch(Exception e){
17+
System.out.println("Invalid");
18+
}
19+
testCases--;
20+
}
21+
}
2022
}

0 commit comments

Comments
 (0)