-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringsProblems.java
More file actions
206 lines (162 loc) · 5.63 KB
/
StringsProblems.java
File metadata and controls
206 lines (162 loc) · 5.63 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import java.util.*;
public class StringsProblems {
public static boolean isPalindrome(String str){
for(int i=0; i<str.length()/2; i++){
int n = str.length();
if(str.charAt(i) != str.charAt(n-1-i)){
return false;
}
}
return true;
}
// Shortest Path
public static float getPath(String path){
int x=0, y=0;
for(int i=0; i<path.length(); i++){
if(path.charAt(i) == 'N'){
y++;
} else if(path.charAt(i ) == 'S'){
y--;
} else if(path.charAt(i) == 'E'){
x++;
} else if(path.charAt(i) == 'W'){
x--;
}
}
int X2 = x*x;
int Y2 = y*y;
double result = Math.sqrt(X2 + Y2);
return (float)result;
}
// Substring using loop
public static String substring(String str, int si, int ei){
String substr = "";
for(int i=si; i<ei; i++){
substr += str.charAt(i);
}
return substr;
}
// Capitalize each word
public static String toUpperCase(String str){
StringBuilder sb = new StringBuilder("");
char ch = Character.toUpperCase(str.charAt(0));
sb.append(ch);
for(int i=1; i<str.length(); i++){
if(str.charAt(i) == ' ' && i<str.length()-1){
sb.append(str.charAt(i));
i++;
sb.append(Character.toUpperCase(str.charAt(i)));
} else {
sb.append(str.charAt(i));
}
}
return sb.toString();
}
// String Compression
public static String stringCompression(String str){
StringBuilder sb = new StringBuilder("");
char currChar = str.charAt(0);
int count = 1;
for(int i=1; i<str.length(); i++){
if(str.charAt(i) == currChar){
count++;
} else {
sb.append(currChar);
if(count > 1){
sb.append(count);
}
currChar = str.charAt(i);
count = 1;
}
}
sb.append(currChar);
if(count > 1){
sb.append(count);
}
return sb.toString();
}
// Count lowercase vowels
public static int lowercaseVowels(String vowels){
int count = 0;
for(int i=0; i<vowels.length(); i++){
if(vowels.charAt(i) == 'a' || vowels.charAt(i) == 'e' || vowels.charAt(i) == 'i' || vowels.charAt(i) == 'o' || vowels.charAt(i) == 'u'){
count++;
}
}
return count;
}
// Anagrams
public static boolean isAnagrams(String str, String str2) {
if (str.length() != str2.length()) {
return false;
}
char[] str1Array = str.toLowerCase().toCharArray();
char[] str2Array = str2.toLowerCase().toCharArray();
Arrays.sort(str1Array);
Arrays.sort(str2Array);
return Arrays.equals(str1Array, str2Array);
}
public static void main(String[] args){
// Anagrams
// String str = "listen";
// String str2 = "silent";
// System.out.println(isAnagrams(str, str2));
// Count lowercase vowels
// Scanner sc = new Scanner(System.in);
// String vowels = sc.nextLine();
// System.out.println(lowercaseVowels(vowels));
// String Compression
// String str = "aaabbcccdd";
// System.out.println(stringCompression(str));
// Capitalize each word
// String str = "hi, i am a learning java";
// System.out.println(toUpperCase(str));
// StringBuilder sb = new StringBuilder("");
// for(char ch = 'a'; ch <= 'z'; ch++){
// sb.append(ch+" ");
// }
// sb.toString();
// System.out.println(sb);
// System.out.println(sb.length());
// largest value
// String fruits[] = {"apple", "mango", "banana"};
// String largest = fruits[0];
// for(int i = 1; i < fruits.length; i++){
// if(largest.compareTo(fruits[i]) < 0){
// // if current 'largest' comes before fruits[i], update
// largest = fruits[i];
// }
// }
// System.out.println("Largest fruit (lexicographically): " + largest);
// Substring using inbuilt function
// String str = "HelloWorld";
// System.out.println(str.substring(0,4));
// Substring using loop
// String str = "HelloWorld";
// System.out.println(substring(str, 0, 4));
// String path = "WNEENESENNN"; // Shortest path
// String str = "lol"; // Palindrome
// String s1 = "Tony"; // String comparison
// String s2 = "Tony"; // String comparison
// String s3 = new String("Tony"); // String comparison
// if(s1 == s2){
// System.out.println("s1 and s2 are same");
// } else {
// System.out.println("s1 and s2 are different");
// }
// if(s1 == s3){
// System.out.println("s1 and s3 are same");
// } else {
// System.out.println("s1 and s3 are different");
// }
// if(s1.equals(s3)){
// System.out.println("s1 and s3 are same");
// } else {
// System.out.println("s1 and s3 are different");
// }
// Palindrome
// System.out.println(isPalindrome(str));
// Shortest path
// System.out.println(getPath(path));
}
}