Several exercises to practice string manipulation
1108. Defanging an IP Address
Given a valid (IPv4) IP address
, return a defanged version of that IP address.
A defanged IP address replaces every period "."
with "[.]"
.
Example 1:
Input: address = "1.1.1.1"
Output: "1[.]1[.]1[.]1"
Example 2:
Input: address = "255.100.50.0"
Output: "255[.]100[.]50[.]0"
Constraints:
- The given
address
is a valid IPv4 address.
Solution:
class Solution {
public String defangIPaddr(String address) {
StringBuilder st = new StringBuilder();
for(int i =0; i< address.length();i++){
if(address.charAt(i) == '.' ){
st.append("[.]");
}else{
st.append(address.charAt(i) );
}
}
return st.toString();
}
}
Exercise #2
1678. Goal Parser Interpretation
You own a Goal Parser that can interpret a string command
. The command
consists of an alphabet of "G"
, "()"
and/or "(al)"
in some order. The Goal Parser will interpret "G"
as the string "G"
, "()"
as the string "o"
, and "(al)"
as the string "al"
. The interpreted strings are then concatenated in the original order.
Given the string command
, return the Goal Parser's interpretation of command
.
Example 1:
Input: command = "G()(al)"
Output: "Goal"
Explanation: The Goal Parser interprets the command as follows:
G -> G
() -> o
(al) -> al
The final concatenated result is "Goal".
Example 2:
Input: command = "G()()()()(al)"
Output: "Gooooal"
Example 3:
Input: command = "(al)G(al)()()G"
Output: "alGalooG"
Constraints:
1 <= command.length <= 100
command
consists of"G"
,"()"
, and/or"(al)"
in some order.
Solution:
class Solution {
public String interpret(String command) {
int i =0;
StringBuilder st = new StringBuilder();
while(i <command.length() ){
if(command.charAt(i)=='G' ){ //G
st.append("G");
i++;
}else if("()".equals(command.substring(i,i+2)) ){ // ()
st.append("o");
i+=2;
}else if("(al)".equals(command.substring(i,i+4)) ){ // ()
st.append("al");
i+=4;
}
}
return st.toString();
}
}
Exercise #3
1684. Count the Number of Consistent Strings
You are given a string allowed
consisting of distinct characters and an array of strings words
. A string is consistent if all characters in the string appear in the string allowed
.
Return the number of consistent strings in the array words
.
Example 1:
Input: allowed = "ab", words = ["ad","bd","aaab","baa","badab"]
Output: 2
Explanation: Strings "aaab" and "baa" are consistent since they only contain characters 'a' and 'b'.
Example 2:
Input: allowed = "abc", words = ["a","b","c","ab","ac","bc","abc"]
Output: 7
Explanation: All strings are consistent.
Example 3:
Input: allowed = "cad", words = ["cc","acd","b","ba","bac","bad","ac","d"]
Output: 4
Explanation: Strings "cc", "acd", "ac", and "d" are consistent.
Constraints:
1 <= words.length <= 104
1 <= allowed.length <= 26
1 <= words[i].length <= 10
- The characters in
allowed
are distinct. words[i]
andallowed
contain only lowercase English letters.
Solution:
class Solution {
public int countConsistentStrings(String allowed, String[] words) {
boolean[] isValid = new boolean[26];
int cnt=0;
//iterate through allowed chars marking isValid as true
fillAllowed(allowed,isValid);
//iter words to check no char is not allowed.
for(String word: words){
if(isWordAllowed(word,isValid) ) cnt++;
}
return cnt;
}
public boolean isWordAllowed(String word, boolean[] isValid){
for(char c: word.toCharArray() ){
if( !isValid[c-'a'] ) return false;
}
return true;
}
public void fillAllowed(String allowed, boolean[] isValid ){
for(char c: allowed.toCharArray() ){
isValid[c-'a'] = true;
}
}
}
Exercise #4
1221. Split a String in Balanced Strings
Balanced strings are those who have equal quantity of ‘L’ and ‘R’ characters.
Given a balanced string s
split it in the maximum amount of balanced strings.
Return the maximum amount of splitted balanced strings.
Example 1:
Input: s = "RLRRLLRLRL"
Output: 4
Explanation: s can be split into "RL", "RRLL", "RL", "RL", each substring contains same number of 'L' and 'R'.
Example 2:
Input: s = "RLLLLRRRLR"
Output: 3
Explanation: s can be split into "RL", "LLLRRR", "LR", each substring contains same number of 'L' and 'R'.
Example 3:
Input: s = "LLLLRRRR"
Output: 1
Explanation: s can be split into "LLLLRRRR".
Example 4:
Input: s = "RLRRRLLRLL"
Output: 2
Explanation: s can be split into "RL", "RRRLLRLL", since each substring contains an equal number of 'L' and 'R'
Constraints:
1 <= s.length <= 1000
s[i] = 'L' or 'R'
Solution:
class Solution {
public int balancedStringSplit(String s) {
int i=0;
int rcount=0,
lcount=0;
int count =0;
while(i < s.length() ){
if(s.charAt(i)=='L' )lcount++;
if(s.charAt(i)=='R' )rcount++;
if( lcount==rcount ){
count++;
lcount=0;
rcount=0;
}
i++;
}
return count;
}
}