LmaoSuVuong
Senior Member
xin tên cty ạĐợi tui luyện leetcode thạo dạng này đã rồi đứa nào phỏng vấn vào công ty tui, tui sẽ đè ra hỏi
xin tên cty ạĐợi tui luyện leetcode thạo dạng này đã rồi đứa nào phỏng vấn vào công ty tui, tui sẽ đè ra hỏi
Ở HN thì pm nhéxin tên cty ạ
impl Solution {
pub fn wonderful_substrings(word: String) -> i64 {
let bytes = word.as_bytes();
let n = bytes.len();
let mut count = 0;
let mut mask_freq = vec![0; 2usize.pow(10)];
mask_freq[0] = 1;
let mut acc = 0u32;
for &c in bytes.iter() {
let char_mask = 1 << (c - b'a');
acc ^= char_mask;
count += mask_freq[acc as usize];
mask_freq[acc as usize] += 1;
for odd_char in (b'a'..=b'j') {
let odd_char_mask = 1 << (odd_char - b'a');
count += mask_freq[(acc ^ odd_char_mask) as usize];
}
}
count
}
}
class Solution {
/**
* @param String $word
* @param String $ch
* @return String
*/
function reversePrefix($word, $ch) {
$rev = '';
$remain = '';
$found = false;
for ($i=0; $i<strlen($word); $i++) {
if (!$found) $rev = $word[$i] . $rev;
else { $remain .= $word[$i]; continue; }
if ($word[$i] == $ch) $found = true;
}
return ($found) ? $rev . $remain : $word;
}
}
class Solution {
public String reversePrefix(String word, char ch) {
char[] w = word.toCharArray();
for(int i = 0;i < w.length;i++){
if(w[i]==ch){
int start= 0;
int end = i;
while(start<end){
char temp = w[start];
w[start]=w[end];
w[end]=temp;
start++;
end--;
}
return new String(w);
}
}
return word;
}
}
class Solution {
public:
string reversePrefix(string word, char ch) {
size_t length = word.length();
size_t ch_pos = word.find(ch);
if (ch_pos == string::npos) return word;
for (int i = 0; i <= ch_pos/2; i++) {
std::swap(word[i], word[ch_pos - i]);
}
return word;
}
};
function reversePrefix(word: string, ch: string): string {
const chIndex = word.indexOf(ch);
if (chIndex === -1) return word;
let reverse = '';
for (let i = chIndex; i >= 0; i--) {
reverse += word[i]
}
reverse += word.slice(chIndex + 1)
return reverse;
};
class Solution(object):
def reversePrefix(self, word, ch):
"""
:type word: str
:type ch: str
:rtype: str
"""
if ch not in word:
return word
return word[:word.index(ch)+1][::-1] + word[word.index(ch)+1:]
use std::str;
impl Solution {
pub fn reverse_prefix(mut word: String, ch: char) -> String {
let chb = ch as u8;
let bytes = unsafe { word.as_bytes_mut() };
if let Some(l) = bytes.iter().position(|&c| c == chb) {
for i in 0..=(l / 2) {
bytes.swap(i, l - i);
}
}
return word;
}
}
public String reversePrefix(String word, char ch) {
int index = word.indexOf(ch);
if (index != -1) {
char[] chars = word.toCharArray();
int l = 0, r = index;
while (l <= r) {
char t = chars[l];
chars[l] = chars[r];
chars[r] = t;
l++;
r--;
}
return new String(chars);
}
return word;
}
public class Solution {
public string ReversePrefix(string word, char ch)
{
int first = word.IndexOf(ch);
if (first == -1)
{
return word;
}
else
{
string res = word.Substring(0, first+1);
return new string(res.Reverse().ToArray()) + word.Substring(first+1);
}
}
}
public String reversePrefix(String word, char ch) {
char[] chars = word.toCharArray();
int left = 0;
int right = word.indexOf(ch);
char temp;
while (left < right) {
temp = chars[left];
chars[left] = chars[right];
chars[right] = temp;
left++;
right--;
}
return String.valueOf(chars);
}