Repository navigation
Expand file tree
/
Copy pathMainClass.java
More file actions
100 lines (83 loc) · 1.78 KB
/
Copy pathMainClass.java
File metadata and controls
100 lines (83 loc) · 1.78 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package strings1;
public class MainClass {
// both i and j are included
static void reverseCharacters(char[] s, int i, int j) {
while(i < j) {
char temp = s[i];
s[i] = s[j];
s[j] = temp;
i++;
j--;
}
}
// start = 11
// end = 13
//
// "Never Give Up"
// "reveN eviG pU"
//
// "Up Give Never"
static String reverseWords(String s) {
char[] c = s.toCharArray();
int start = 0;
int end = 0;
for(; end < s.length(); end++) {
if(c[end] == ' ') {
reverseCharacters(c, start, end-1);
start = end + 1;
}
}
reverseCharacters(c, start, end-1);
//step 2
reverseCharacters(c, 0, s.length()-1);
return new String(c);
}
static boolean anagrams(String s1, String s2) {
int a[] = new int[256];
for(int i = 0; i<s1.length(); i++) {
a[s1.charAt(i)]++;
}
for(int e: a) {
System.out.print(e+" ");
}
System.out.println();
for(int i = 0; i<s2.length(); i++) {
a[s2.charAt(i)]--;
}
for(int e: a) {
System.out.print(e+" ");
}
for(int e: a) {
if(e != 0) return false;
}
return true;
}
public static void main(String[] args) {
// String s1 = "abcda";
// String s2 = "adacasdkfhalksdjfalsdkjfc";
//
// System.out.println(anagrams(s1, s2));
String s = "Never Give Up";
String ans = reverseWords(s);
System.out.println(ans);
// String s1 = "Hello";
// String s2 = "hello";
// String s3 = new String("Hello");
//
// System.out.println(s1 == s2);
// System.out.println(s1 == s3);
// System.out.println(s1.equals(s3));
//
// String s4 = s1+"abc";
//
// String s5 = s1.concat("pqr").replace('H', 'b');
////
// System.out.println(s1);
//// System.out.println(s5);
//
// String s6 = s1.toUpperCase();
//
// System.out.println("s1 " + s1);
// System.out.println("s6 " + s6);
}
}