Skip to content

Commit a9c07a2

Browse files
Added String Problems
1 parent 796686f commit a9c07a2

13 files changed

+379
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.String_Problems;
2+
3+
public class _01_Adding_Characters_to_String {
4+
public static void main(String[] args) {
5+
String series = "";
6+
for (int i = 0; i < 26; i++) { // for loop is running for all 26 alphabets here
7+
char ch = (char)('a' + i); // In ASCII value of a value of i is added Eg.: 97(a) + 1 = 98(b)
8+
System.out.println(ch);
9+
series = series + ch;
10+
}
11+
System.out.println(series);
12+
}
13+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.String_Problems;
2+
3+
public class _02_String_Builder {
4+
public static void main(String[] args) {
5+
StringBuilder builder = new StringBuilder();
6+
for (int i = 0; i < 26; i++) { // for loop is running for all 26 alphabets here
7+
char ch = (char)('a' + i); // In ASCII value of a value of i is added Eg.: 97(a) + 1 = 98(b)
8+
builder.append(ch);
9+
}
10+
System.out.println(builder);
11+
System.out.println(builder.toString());
12+
builder.deleteCharAt(0); // 1st character removed
13+
System.out.println(builder);
14+
15+
}
16+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.String_Problems;
2+
3+
/*
4+
Check whether String is palindrome or not
5+
Eg1: abcdcba (true , it is palindrome)
6+
Eg2: abccab (false , it is not palindrome)
7+
*/
8+
public class _03_Check_Palindrome {
9+
public static void main(String[] args) {
10+
String str = "null";
11+
System.out.println(isPalindrome(str));
12+
}
13+
static boolean isPalindrome(String str) {
14+
if(str == null || str.length() == 0) {
15+
return true;
16+
}
17+
str = str.toLowerCase();
18+
for(int i = 0; i<= str.length() / 2; i++) {
19+
char start = str.charAt(i);
20+
char end = str.charAt(str.length() - 1 - i);
21+
22+
if(start != end) {
23+
return false;
24+
}
25+
}
26+
return true;
27+
}
28+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.String_Problems;
2+
/*
3+
Given a valid (IPv4) IP address, return a defanged version of that IP address.
4+
A defanged IP address replaces every period "." with "[.]".
5+
6+
Example 1:
7+
Input: address = "1.1.1.1"
8+
Output: "1[.]1[.]1[.]1"
9+
*/
10+
public class _04_Defanging_an_IP_Address {
11+
public static void main(String[] args) {
12+
String IPAddress = "255.100.50.0";
13+
System.out.println(defangIPaddr(IPAddress));
14+
}
15+
static String defangIPaddr(String address) {
16+
StringBuilder str = new StringBuilder();
17+
for(int i=0; i<address.length();i++)
18+
{
19+
if(address.charAt(i)=='.')
20+
str.append("[.]");
21+
else
22+
str.append(address.charAt(i));
23+
}
24+
25+
return str.toString();
26+
}
27+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.String_Problems;
2+
/*
3+
You are given a string s and an integer array indices of the same length.
4+
The string s will be shuffled such that the character at the ith position moves to indices[i] in the shuffled string.
5+
Return the shuffled string.
6+
7+
Eg-1:
8+
Input: s = "codeleet", indices = [4,5,6,7,0,2,1,3]
9+
Output: "leetcode"
10+
Explanation: As shown, "codeleet" becomes "leetcode" after shuffling.
11+
*/
12+
public class _05_Shuffle_String {
13+
public static void main(String[] args) {
14+
String name = "hilSa";
15+
int[] name_index = {2,3,4,0,1};
16+
System.out.println(restoreString(name,name_index));
17+
}
18+
static String restoreString(String s, int[] indices) {
19+
// Converting String s into Character Array
20+
char[] ch=s.toCharArray();
21+
22+
for(int i=0;i<indices.length;i++){
23+
ch[indices[i]]=s.charAt(i);
24+
}
25+
26+
// Converting Array into String
27+
28+
s=String.valueOf(ch);
29+
return s;
30+
}
31+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.String_Problems;
2+
/*
3+
You own a Goal Parser that can interpret a string command.
4+
The command consists of an alphabet of "G", "()" and/or "(al)" in some order.
5+
The Goal Parser will interpret "G" as the string "G", "()" as the string "o", and "(al)" as the string "al".
6+
The interpreted strings are then concatenated in the original order.
7+
Given the string command, return the Goal Parser's interpretation of command.
8+
9+
Eg: Input: command = "G()()()()(al)"
10+
Output: "Gooooal"
11+
*/
12+
public class _06_Goal_Parser_Interpretation {
13+
public static void main(String[] args) {
14+
String goal = "G()(al)()()";
15+
System.out.println(interpret(goal));
16+
}
17+
static String interpret(String command) {
18+
StringBuilder list = new StringBuilder();
19+
for(int i = 0; i < command.length(); i++){
20+
if(command.charAt(i) == 'G'){
21+
list.append("G");
22+
}else if(command.charAt(i) == '(' && command.charAt(i + 1) == ')'){
23+
list.append("o");
24+
}else if(command.charAt(i) == '(' && command.charAt(i + 1) == 'a'){
25+
list.append("al");
26+
}
27+
}
28+
String ans = list.toString();
29+
return ans;
30+
}
31+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.String_Problems;
2+
/*
3+
Given two string arrays word1 and word2,
4+
return true if the two arrays represent the same string, and false otherwise.
5+
A string is represented by an array if the array elements concatenated in order forms the string.
6+
7+
Example 1:
8+
Input: word1 = ["ab", "c"], word2 = ["a", "bc"]
9+
Output: true
10+
*/
11+
public class _07_Check_2_Strings_are_equal {
12+
public static void main(String[] args) {
13+
String[] word1 = {"ab", "c", "d", "e"};
14+
String[] word2 = {"a", "bc", "de"};
15+
System.out.println(arrayStringsAreEqual(word1, word2));
16+
}
17+
static boolean arrayStringsAreEqual(String[] word1, String[] word2) {
18+
String str1 = new String();
19+
String str2 = new String();
20+
21+
for (int i = 0; i < word1.length; i++) {
22+
str1 = str1 + word1[i];
23+
}
24+
25+
for (int j = 0; j < word2.length; j++) {
26+
str2 = str2 + word2[j];
27+
}
28+
return str1.equals(str2);
29+
}
30+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.String_Problems;
2+
/*
3+
You are given a string s of even length. Split this string into two halves of equal lengths,
4+
and let a be the first half and b be the second half.
5+
Two strings are alike if they have the same number of vowels
6+
('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U').
7+
Notice that s contains uppercase and lowercase letters.
8+
Return true if a and b are alike. Otherwise, return false.
9+
10+
Example 1:
11+
Input: s = "book"
12+
Output: true
13+
Explanation: a = "bo" and b = "ok". a has 1 vowel and b has 1 vowel. Therefore, they are alike.
14+
*/
15+
public class _08_String_Halves_are_alike {
16+
public static void main(String[] args) {
17+
String text = "book";
18+
System.out.println(halvesAreAlike(text));
19+
}
20+
static boolean halvesAreAlike(String s) {
21+
String s1 = s.substring(0, s.length()/2);
22+
String s2 = s.substring(s.length()/2);
23+
if(checkVowel(s1) == checkVowel(s2)) {
24+
return true;
25+
}
26+
return false;
27+
}
28+
static int checkVowel(String s) {
29+
int count = 0;
30+
for (int i = 0; i < s.length(); i++) {
31+
char ch = s.charAt(i);
32+
if(ch == 'a' || ch=='e'||ch=='i' || ch=='o'||ch=='u'||ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U') {
33+
count++;
34+
}
35+
}
36+
return count;
37+
}
38+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.String_Problems;
2+
/*
3+
Given an array of strings patterns and a string word,
4+
return the number of strings in patterns that exist as a substring in word.
5+
A substring is a contiguous sequence of characters within a string.
6+
Example 1:
7+
Input: patterns = ["a","abc","bc","d"], word = "abc"
8+
Output: 3
9+
*/
10+
public class _09_Strings_appear_as_Substring_in_word {
11+
public static void main(String[] args) {
12+
String[] pattern = {"a","abc","bc","d","c"};
13+
String word = "abc";
14+
System.out.println(numOfStrings(pattern, word));
15+
}
16+
static int numOfStrings(String[] patterns, String word) {
17+
int count = 0;
18+
for (int i = 0; i < patterns.length; i++) {
19+
if(word.contains(patterns[i])) {
20+
count++;
21+
}
22+
}
23+
return count;
24+
}
25+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.String_Problems;
2+
/*
3+
There is a robot starting at the position (0, 0), the origin, on a 2D plane.
4+
Given a sequence of its moves, judge if this robot ends up at (0, 0) after it completes its moves.
5+
You are given a string moves that represents the move sequence of the robot
6+
where moves[i] represents its ith move. Valid moves are 'R' (right), 'L' (left), 'U' (up), and 'D' (down).
7+
Return true if the robot returns to the origin after it finishes all of its moves, or false otherwise.
8+
Note: The way that the robot is "facing" is irrelevant. 'R' will always make
9+
the robot move to the right once, 'L' will always make it move left, etc.
10+
Also, assume that the magnitude of the robot's movement is the same for each move.
11+
12+
Example 1:
13+
Input: moves = "UD"
14+
Output: true
15+
16+
Example 2:
17+
Input: moves = "LL"
18+
Output: false
19+
*/
20+
public class _10_Robot_Return_to_Origin {
21+
public static void main(String[] args) {
22+
String move = "LLRURD";
23+
System.out.println(judgeCircle(move));
24+
}
25+
static boolean judgeCircle(String moves) {
26+
int x = 0;
27+
int y = 0;
28+
for(char move: moves.toCharArray()) {
29+
if(move == 'U') {
30+
y++;
31+
} else if(move == 'D') {
32+
y--;
33+
} else if(move == 'L') {
34+
x--;
35+
} else if(move == 'R') {
36+
x++;
37+
}
38+
}
39+
return (x == 0 && y == 0);
40+
}
41+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.String_Problems;
2+
/*
3+
Given a string s, reverse the order of characters in each word within a sentence
4+
while still preserving whitespace and initial word order.
5+
6+
Example 1:
7+
Input: s = "Let's take LeetCode contest"
8+
Output: "s'teL ekat edoCteeL tsetnoc"
9+
*/
10+
public class _11_Reverse_String_Words_Only {
11+
public static void main(String[] args) {
12+
String text = "Let's Study Today";
13+
System.out.println(reverseWords(text));
14+
}
15+
static String reverseWords(String s) {
16+
StringBuilder sb = new StringBuilder(s);
17+
sb.reverse();
18+
String[] result = sb.toString().split(" ");
19+
for(int i=0,j=result.length-1;i<=j;i++,j--){
20+
String temp = result[j];
21+
result[j] = result[i];
22+
result[i] = temp;
23+
}
24+
return String.join(" ", result);
25+
}
26+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.String_Problems;
2+
/*
3+
Your friend is typing his name into a keyboard. Sometimes,when typing a character c,
4+
the key might get long pressed, and the character will be typed 1 or more times.
5+
You examine the typed characters of the keyboard.
6+
Return True if it is possible that it was your friends name,
7+
with some characters (possibly none) being long pressed.
8+
9+
Example 1:
10+
Input: name = "alex", typed = "aaleex"
11+
Output: true
12+
*/
13+
public class _12_Long_Pressed_Name {
14+
public static void main(String[] args) {
15+
String name = "sahil";
16+
String typed = "ssaahill";
17+
System.out.println(isLongPressedName(name, typed));
18+
}
19+
static boolean isLongPressedName(String name, String typed) {
20+
int i = 0, j = 0;
21+
while (j < typed.length()) {
22+
if(i != name.length() && name.charAt(i) == typed.charAt(j)) {
23+
i++;
24+
j++;
25+
}
26+
else if(i > 0 && name.charAt(i - 1) == typed.charAt(j)) {
27+
j++;
28+
} else {
29+
return false;
30+
}
31+
}
32+
if(i == name.length()) { return true; }
33+
return false;
34+
}
35+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.String_Problems;
2+
/*
3+
Given two non-negative integers, num1 and num2 represented as string,
4+
return the sum of num1 and num2 as a string.
5+
You must solve the problem without using any built-in library for handling large integers (such as BigInteger).
6+
You must also not convert the inputs to integers directly.
7+
8+
Example 1:
9+
Input: num1 = "11", num2 = "123"
10+
Output: "134"
11+
*/
12+
public class _13_Add_Strings {
13+
public static void main(String[] args) {
14+
String num1 = "123";
15+
String num2 = "346";
16+
System.out.println(addStrings(num1, num2));
17+
}
18+
static String addStrings(String num1, String num2) {
19+
int n1=num1.length()-1;
20+
int n2=num2.length()-1;
21+
int carry=0;
22+
StringBuilder ans=new StringBuilder();
23+
while(n1>=0 || n2>=0){
24+
int sum =carry;
25+
if(n1>=0) {
26+
sum+=num1.charAt(n1--)-'0';
27+
}
28+
if(n2>=0) {
29+
sum+=num2.charAt(n2--)-'0';
30+
}
31+
ans.append(sum%10);
32+
carry=sum/10;
33+
}
34+
if(carry!=0)
35+
ans.append(carry);
36+
return ans.reverse().toString();
37+
}
38+
}

0 commit comments

Comments
 (0)