Skip to content

Commit

Permalink
adding 2 more solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
abhishektripathi66 committed Dec 3, 2024
1 parent 1aefbf9 commit 1f17214
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 0 deletions.
69 changes: 69 additions & 0 deletions src/Coding Questions/Leetcode/AddingSpacestoaString.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* 2109. Adding Spaces to a String
Solved
Medium
Topics
Companies
Hint
You are given a 0-indexed string s and a 0-indexed integer array spaces that describes the indices in the original string where spaces will be added. Each space should be inserted before the character at the given index.
For example, given s = "EnjoyYourCoffee" and spaces = [5, 9], we place spaces before 'Y' and 'C', which are at indices 5 and 9 respectively. Thus, we obtain "Enjoy Your Coffee".
Return the modified string after the spaces have been added.
Example 1:
Input: s = "LeetcodeHelpsMeLearn", spaces = [8,13,15]
Output: "Leetcode Helps Me Learn"
Explanation:
The indices 8, 13, and 15 correspond to the underlined characters in "LeetcodeHelpsMeLearn".
We then place spaces before those characters.
Example 2:
Input: s = "icodeinpython", spaces = [1,5,7,9]
Output: "i code in py thon"
Explanation:
The indices 1, 5, 7, and 9 correspond to the underlined characters in "icodeinpython".
We then place spaces before those characters.
Example 3:
Input: s = "spacing", spaces = [0,1,2,3,4,5,6]
Output: " s p a c i n g"
Explanation:
We are also able to place spaces before the first character of the string.
Constraints:
1 <= s.length <= 3 * 105
s consists only of lowercase and uppercase English letters.
1 <= spaces.length <= 3 * 105
0 <= spaces[i] <= s.length - 1
All the values of spaces are strictly increasing.
*/

public class AddingSpacestoaString {

public String addSpaces(String s, int[] spaces) {
char[] a = s.toCharArray();
char[] b = new char[s.length()+spaces.length];

if( spaces.length==0) return s;
int j=0;
int k=0;
for(int i:spaces){
while(j<i){
b[k++]=a[j++];
}
b[k++]=' ';
}

while(j<s.length()){
b[k++]=a[j++];
}

return String.valueOf(b);

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* 1455. Check If a Word Occurs As a Prefix of Any Word in a Sentence
Solved
Easy
Topics
Companies
Hint
Given a sentence that consists of some words separated by a single space, and a searchWord, check if searchWord is a prefix of any word in sentence.
Return the index of the word in sentence (1-indexed) where searchWord is a prefix of this word. If searchWord is a prefix of more than one word, return the index of the first word (minimum index). If there is no such word return -1.
A prefix of a string s is any leading contiguous substring of s.
Example 1:
Input: sentence = "i love eating burger", searchWord = "burg"
Output: 4
Explanation: "burg" is prefix of "burger" which is the 4th word in the sentence.
Example 2:
Input: sentence = "this problem is an easy problem", searchWord = "pro"
Output: 2
Explanation: "pro" is prefix of "problem" which is the 2nd and the 6th word in the sentence, but we return 2 as it's the minimal index.
Example 3:
Input: sentence = "i am tired", searchWord = "you"
Output: -1
Explanation: "you" is not a prefix of any word in the sentence.
Constraints:
1 <= sentence.length <= 100
1 <= searchWord.length <= 10
sentence consists of lowercase English letters and spaces.
searchWord consists of lowercase English letters.
*
*/

public class CheckIfaWordOccursAsaPrefixofAnyWordinaSentence {

public int isPrefixOfWord(String sentence, String searchWord) {

String[] s = sentence.split(" ");
for(int i=0;i<s.length;i++){
if(s[i].startsWith(searchWord)) return i+1;
}

return -1;

}

}

0 comments on commit 1f17214

Please sign in to comment.